An automatic way to create gameobjects in Unity? - c#

Basically is there an actually time saving way to creat a function that lets you create a GameObject specified by the function's parameter(s)?
like:
public void thing_maker(string gameobject_name, string sprite_name, string rg_body_name)

this example should do what you need.
it requires that you have a folder named Resources and that the sprite you want to load will be inside that folder.
another option is doing GameObject go = Instantite(SomePrefabName) as GameObject insted of new GameObject(), in case you have a prefab which is ready and you only want to maybe change some of its components' values.
Good luck.
public GameObject thing_maker(string gameobject_name, string sprite_name, string rg_body_name)
{
GameObject go = new GameObject(gameobject_name);
SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
sr.sprite = Resources.Load<Sprite>(sprite_name);
Rigidbody rb = go.AddComponent<Rigidbody>();
/* now if you want to change values of the components you can just do it
by accesing them directly. for instance: rb.isKinematic = true; will
change the isKinematic value of this rigidbody to true. */
return go;
}

It seems to me that you could try something like this:
public void ThingMaker(string gameobject_name, Sprite spriteToDraw) {
GameObject newObj;
newObj= new GameObject(gameobject_name);
newObj.AddComponent<Rigidbody2D>();
newObj.AddComponent<SpriteRenderer>();
newObj.GetComponent<SpriteRenderer>().sprite = spriteToDraw;
}

Related

gameObject Transform in DontDestroyOnLoad

I use the following script to copy the transform of a gameObject.
public Transform alignment;
void Update()
{
DontDestroyOnLoad(transform.gameObject);
transform.position = new Vector3(alignment.position.x, alignment.position.y, alignment.position.z);
float y = alignment.transform.localRotation.eulerAngles.y;
float x = alignment.transform.localRotation.eulerAngles.x;
float z = alignment.transform.localRotation.eulerAngles.z;
transform.localEulerAngles = new Vector3(x, y, z);
}
I want to acces this transform in other scenes (apply it to gameObjects).
How can I reach the transforms, that are in DontDestroyOnLoad?
First of all, don't use DontDestroyOnLoad in your update function. Use it in your Start or Awake funciton.
Secondly you can access your Non-Destroyed object like you would any other object.
Only differences between the two is that the Non-Destroyed stays in the scene when changing scenes.
Here are some ways to access it:
var obj = GameObject.Find("My Non Destroyed Object Name");
Or my personal favorite, if the object is used for scoring or settings I'll create a new gameobject and add the following script to it:
public class GameManager : MonoDevelop {
/* These are all script on the same gameobject that is not being destroyed */
public static Settings settings;
public static PlayerMananger playerMananger;
public static UIManager uiManager;
void Start() {
DontDestroyOnLoad(this.gameobject);
// Get the components
settings = GetComponent<Settings>();
playerMananger = GetComponent<PlayerMananger>();
uiManager = GetComponent<UIManager>();
}
}
Now I can create a script from anywhere and I can access all these Manager and setting files simply using:
var settings = GameManager.settings;
You need to give alignment as parameter to the DontDestroyOnLoad method, not transform.gameObject.
DontDestroyOnLoad(alignment.gameObject);
Also, you don't need to call this method inside Update(). Just calling it once it enough. You can call it inside Start(), like this:
void Start()
{
DontDestroyOnLoad(alignment.gameObject);
}
Now your alignment gameobject will still be accessible when you change scenes. If you want to access it from other gameobjects in the new scene, you might want to give it a tag or make it a singleton.

Can only spawn one object at once in unity

In my game I have a game object called ExclamationMark which I want to spawn above enemies heads when the player gets into range and they become "Alerted".
I've made this simple script to do that, but for some reason it will only work on one game object.
My enemy script:
void CheckForPlayer()
{
// Define player and get position
var player = GameObject.FindWithTag("Player");
var playerPos = (int)player.transform.position.x;
if (transform.Find("Graphics"))
{
// Define gameobject position
var enemyPos = transform.Find("Graphics").gameObject.transform.position.x;
// Define range to spawn tiles in
var range = 5;
var rangeInfront = enemyPos + range;
var rangeBehind = enemyPos - range;
if (playerPos >= rangeBehind && playerPos <= rangeInfront)
{
enemyIsActive = true;
if (transform.Find("ExclamationMark"))
{
var exMark = transform.Find("ExclamationMark").gameObject.GetComponent<ExclamationMarkSpawn>();
exMark.SpawnExclamationMark();
}
}
else
{
enemyIsActive = false;
}
}
}
My ! script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExclamationMarkSpawn : MonoBehaviour {
public GameObject spawnPos;
public GameObject exclamationMark;
public GameObject exclamationMarkAudio;
public void SpawnExclamationMark()
{
StartCoroutine(GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraShake>().Shake(0.2f, 0.2f, 0.2f));
Instantiate(exclamationMark, spawnPos.transform.position, Quaternion.identity);
if (exclamationMarkAudio)
Instantiate(exclamationMarkAudio, spawnPos.transform.position, Quaternion.identity);
StartCoroutine(DestroyExclamationMark());
}
IEnumerator DestroyExclamationMark()
{
yield return new WaitForSeconds(1);
var children = new List<GameObject>();
foreach (Transform child in transform) children.Add(child.gameObject);
children.ForEach(child => Destroy(child));
}
}
Just to be sure: I assume every player has its own instance of both of your scripts attached (some maybe nested further in their own hierarchy).
I assume that since you are using transform.Find which looks for the object by name within it's own children.
In general using Find and GetComponent over and over again is very inefficient! You should in both classes rather store them to fields and re-use them. Best would be if you can actually already reference them via the Inspector and not use Find and GetComponent at all.
In general finding something by name is always error prone. Are you sure they are all called correctly? Or are others maybe further nested?
Note: Find does not perform a recursive descend down a Transform hierarchy.
I would prefer to go by the attached components. You say it has e.g. a RigidBody. If this is the only Rigidbody component in the hierarchy below your objects (usually this should be the case) then you could instead rather simply use
// pass in true to also get disabled or inactive children
Rigidbody graphics = GetComponentInChildren<Rigidbody>(true);
the same for the ExclamationMarkSpawn
// Would be even beter if you already reference these in the Inspector
[SerializeField] private Rigidbody graphics;
[SerializeField] private ExclamationMarkSpawn exclamationMark;
[SerializeField] private Transform player;
private void Awake()
{
if(!player) player = GameObject.FindWithTag("Player");
if(!graphics) graphics = GetComponentInChildren<Rigidbody>(true);
if(!exclamationMark) exclamationMark = GetComponentInChildren<ExclamationMarkSpawn>(true);
}
private void CheckForPlayer()
{
// If really needed you can also after Awake still use a lazy initialization
// this adds a few later maybe unnecessary if checks but is still
// cheaper then using Find over and over again
if(!player) player = FindWithTag("Player");
if(!graphics) graphics = GetComponentInChildren<Rigidbody>(true);
if(!exclamationMark) exclamationMark = GetComponentInChildren<ExclamationMarkSpawn>(true);
var playerPos = (int)player.position.x;
// always if making such a check also give a hint that something might be missing
if (!graphics)
{
// by adding "this" you can now simply click on the message
// in the console and it highlights the object where this is happening in the hierarchy
Debug.LogWarning("graphics is missing here :'( ", this);
return;
}
// Define gameobject position
var enemyPos = graphics.transform.position.x;
// Define range to spawn tiles in
// this entire block can be shrinked down to
if (Mathf.Abs(playerPos - enemyPos) <= 5)
{
enemyIsActive = true;
if (exclamationMark) exclamationMark.SpawnExclamationMark();
}
else
{
enemyIsActive = false;
}
}
The same also in ExclamationMarkSpawn.cs.
I would additionally only allow 1 exclamation mark being visible at the same time. For example when a player jitters in the distance especially assuming both, the player and the enemy, I would move the entire instantiation to the routine and use a flag. Especially since this is called every frame in Update while the player stays in the range!
Also re-check and make sure your enemies are not maybe referencing the same spawnPos and thus all instantiating their exclamation marks on top of each other.
public class ExclamationMarkSpawn : MonoBehaviour
{
public Transform spawnPos;
public GameObject exclamationMark;
public GameObject exclamationMarkAudio;
[SerializeField] private CameraShake cameraShake;
// only serialized for debug
[SerializeField] private bool isShowingExclamation;
private void Awake()
{
if(!cameraShake) cameraShake = Camera.main.GetComponent<CameraShake>();
// or assuming this component exists only once in the entire scene anyway
if(!cameraShake) cameraShake = FindObjectOfType<CameraShake>();
}
public void SpawnExclamationMark()
{
StartCoroutine(ShowExclamationMark());
}
private IEnumerator ShowExclamationMark()
{
// block concurrent routine call
if(isShowingExclamation) yield brake;
// set flag blocking concurrent routines
isShowingExclamation = true;
// NOTE: Also for this one you might want to rather have a flag
// multiple enemy instances might call this so you get concurrent coroutines also here
StartCoroutine(cameraShake.Shake(0.2f, 0.2f, 0.2f));
Instantiate(exclamationMark, spawnPos.position, Quaternion.identity);
if (exclamationMarkAudio) Instantiate(exclamationMarkAudio, spawnPos.position, Quaternion.identity);
yield return new WaitForSeconds(1);
var children = new List<GameObject>();
foreach (var child in transform.ToList()) children.Add(child.gameObject);
children.ForEach(child => Destroy(child));
// give the flag free
isShowingExclamation = false;
}
}
Try this;
if (transform.Find("ExclamationMark"))
{
var exMark = transform.Find("ExclamationMark").gameObject.GetComponent<ExclamationMarkSpawn>();
exMark.SpawnExclamationMark(transform.position); //Add transform.position here
}
public void SpawnExclamationMark(Vector3 EnemyPos)
{
StartCoroutine(GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraShake>().Shake(0.2f, 0.2f, 0.2f));
Instantiate(exclamationMark, EnemyPos, Quaternion.identity);
if (exclamationMarkAudio)
Instantiate(exclamationMarkAudio, EnemyPos, Quaternion.identity);
StartCoroutine(DestroyExclamationMark());
}

How to instantiate an Oculus grabbable object at runtime in Unity?

Right now I have an obj file in Unity. I have a script that adds a Collider component, a Rigidbody component, and finally an OVRGrabbable component to the object. I need to add these components at runtime because eventually I will be producing procedural meshes in a script at runtime, and I need these procedural meshes to be grabbable.
My problem is that the OVRGrabbable script does not recognize the added collider as a grab point when the collider is added at runtime. I thought that it would be enough to add the collider before the OVRGrabbable in my script, but no dice. I tried attaching the collider in an Awake function and then the OVRGrabbable in the Start function, but that didn't work either. Additionally, I cannot add it in script because the grabPoints array is read-only. Here is my code:
public class AddVRComponents : MonoBehaviour {
void Start () {
public bool freeMoving = false;
public bool useGravity = false;
collide = gameObject.AddComponent<BoxCollider>();
Rigidbody rB = gameObject.AddComponent<Rigidbody>();
if (!freeMoving)
{
rB.drag = Mathf.Infinity;
rB.angularDrag = Mathf.Infinity;
}
if (!useGravity)
{
rB.useGravity = false;
}
OVRGrabbable grab = gameObject.AddComponent<OVRGrabbable>();
Collider[] newGrabPoints = new Collider[1];
newGrabPoints[0] = collide;
grab.enabled = true;
grab.grabPoints = newGrabPoints;
}
}
This obviously does not work because the final line produces the error that grab.grabPoints is read-only.
I know that it can be done because if I run my program and then in the editor manually drag my collider into the grab points field of the OVRGrabbable component, the object can be grabbed.
How can I get the OVRGrabbable script to recognize my collider?
Read-only properties are properties that can be assigned only through the script that contains them. This means that you can only change the value of grabPoints from inside the OVRGrabbable.cs script.
The best way to do this is by adding a custom function inside the OVRGrabbable.cs file so that you can access and set the read only variables.
I use this one so I can also set the snapPosition and snapOrientation fields as well as the grabPoints.
public void Initialize(bool snapPosition, bool snapOrientation, Collider[] grabPoints)
{
m_snapPosition = snapPosition;
m_snapOrientation = snapOrientation;
m_grabPoints = grabPoints;
}
You'll be good enough only adding and calling the following one:
public void Initialize(Collider[] grabPoints)
{
m_grabPoints = grabPoints;
}
I made this modification to OVRGrabbale.cs:
void Awake()
{
if (ReferenceEquals(m_grabPoints, null) || m_grabPoints.Length == 0)
{
// Get the collider from the grabbable
Collider collider = this.GetComponent<Collider>();
if (collider == null)
{
throw new ArgumentException("Grabbables cannot have zero grab points and no collider -- please add a grab point or collider.");
}
// Create a default grab point
m_grabPoints = new Collider[1] { collider };
}
}
I checked to see if m_grabPoints is null and it worked.

why GetComponent returns null reference for imageEffect?

this problem is unsolvable or what?i don't need to provide any code example it just doesn't work to anyone!and i don't know how its possible,you just have to try this : GetComponent<DepthOfField>().enabled = false;
assume that we have a MainCamera object and we attach it a DepthOfField image effect Script.and we create another script named whatever and attach it to the MainCamera and in the whatever script we just call this:
void Start() {
GetComponent<DepthOfField>().enabled = false;
}
shouldn't it work?well it doesn't, it returns null reference exception even if the script is attached to the main camera.is it fixable? and if yes i need it !
It should work because they are both attached to the same GameObject. Although, there are many other ways you can try to fix this.
You can just do
DepthOfField myDept;
void Start() {
myDept = GameObject.Find("MainCamera").GetComponent<DepthOfField>();
myDept.enabled = false;
}
Or You could make DepthOfField public then assign the DepthOfField from the Editor. For example,
public DepthOfField myDept;
//Drag and Drop the MainCamera GameObject from the scene to this and it will automatically assign the DepthOfField script to the myDept.
Then you can do
myDept.enabled = false;
For this to work, DepthOfField must be attached to the MainCamera GameObject.

Unity3d MeshRender

I'm trying to use C# to disable and enable the MeshRender component in Unity3d however I am getting the following error,
error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.GetComponent(System.Type)'
The line of code I am using is below. I'm using this in the same function.
MeshRenderer showZone = GameObject.GetComponent<MeshRenderer>();
Also I'm posting here rather than Unity Answers as I get a far faster response here and it's always useful information regardless of the outcome.
You're having trouble with several problems. First, you are trying to use GetComponent<> on a class instead of an instance of an object. This leads directly to your second problem. After searching for a specific GameObject you're not using the result and you're trying to disable the renderer of the GameObject containing the script. Third, C# is case-sensitive, Renderer is a class while renderer is a reference to an instance of Renderer attached to the GameObject
This code snippet combines everything: find the GameObject and disable its renderer
GameObject go = GameObject.FindWithTag("zone1");
if (go != null) { // the result could be null if no matching GameObject is found
go.renderer.enabled = false;
}
You could use go.GetComponent<MeshRenderer>().enabled = false; instead of go.renderer. enabled = false; But by using renderer you don't need to know what kind of renderer is used by the GameObject. It could be a MeshRenderer or a SpriteRenderer for example, renderer always points to the renderer used by the GameObject, if there exists one.
My friend. Just try use lowercase gameObject instead of GameObject and renderer instead of Renderer
The main problem that you try access Static class variable, using the name of class instead of class instance.
Class names here are GameObject and Renderer
And instances are gameObject and renderer
MeshRenderer showZone = GetComponent<MeshRenderer>();
delete the 'GameObject.'
GameObject is a type. What you want is in an instance of a gameObject to call GetcComponent on. Thats what the error is about.
Which for note, this:
MeshRenderer showZone = GetComponent<MeshRenderer>();
is the exact same as this:
MeshRenderer showZone = this.GetComponent<MeshRenderer>();
You are calling GetComponent on the GameObject instance of which the script is attached to.
your code should look like this:
MeshRenderer showZone = GetComponent<MeshRenderer>();
Like others already wrote, you need to get an instantiated GameObject. You call the base class GameObject where only static functions can be called which do not need a GameObject in the SceneView.
gameObject IS AN instance.You get the instance of the GameObject the Monobehaviour is added to. Calling the function GetComponent without any object is the same as:
this
gameObject
GameObject IS NO instance.
Be careful at the first letter!
Look documentation:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Example() {
renderer.enabled = false;
}
}
Link: http://docs.unity3d.com/ScriptReference/Renderer-enabled.html
Changing of programming languages upper-right.
2 ways you can solve the Problem either. You add the word static to the method that is calling your statement.
ex :
public static GetTheMesh(){}
I do not recommend on doing this cause. If you have other calls inside the method that needs to access Instance, this will cause you trouble.
Second way of fixing it is make a pointer or reference first before getting the component. Or use the GameObject.Find <= which is slow if.
showZone = GameObject.Find("TheGameObjectName").GetComponent<MeshRenderer>();
If you want to disable the renderer on this gameObject then use:
this.GetComponent<Renderer>().enabled = false;
If it's not this gameObject then use:
GameObject.FindGameObjectWithTag("your_tag").GetComponent<Renderer>().enabled = false;
Or you could give the object manually:
public GameObject go;
go.GetComponent<Renderer>().enabled = false;
https://docs.unity3d.com/ScriptReference/Renderer-enabled.html
you can use two types of peace of code for access MeshRenderer enable and disable
1> create GetMeshRenderer script (script name as you want) attached to empty game-object into the scene and assign Cube or sphere or any 3d object as u want to enable and disable.
************************************** Code ***************************
using UnityEngine;
using System.Collections;
public class GetMeshRenderer : MonoBehaviour
{
public MeshRenderer ShowZone;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.Y))
{
ShowZone.enabled = true;
}
if(Input.GetKey(KeyCode.N))
{
ShowZone.enabled = false;
}
}
}
2>
attach below peace of code script to any 3d object like sphere ,cube
*************************** code ***************************
using UnityEngine;
using System.Collections;
public class GetMeshRenderer : MonoBehaviour
{
private MeshRenderer ShowZone;
// Use this for initialization
void Start ()
{
ShowZone = gameObject.GetComponent<MeshRenderer> ();
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.Y))
{
ShowZone.enabled = true;
}
if(Input.GetKey(KeyCode.N))
{
ShowZone.enabled = false;
}
}
}
Your problem is that you are using GameObject which is just a class that "describes" what it is. What you want, if this script is attached to the GameObject who's mesh renderer you want, is gameObject with a lowercase "g."
If you want to get the mesh renderer of another GameObject, you can find it by name with GameObject.Find("zone1"); (note the uppercase "G" in that one,) or you can give it a tag and find it with GameObject.FindGameObjectWithTag("zone1");
(You may or may not already know that but it doesn't hurt to provide the information.)
Edit:
Your other problem is that you must use renderer instead of Renderer because, like the GameObject "Renderer" with a capital "R" references a class, instead of a specific object.
the problem is GameObject is different from gameobject Gameobject is a class and gameobject is a instance of current gameobject or gameobject in which the script is attached
Replace the line
MeshRenderer showZone = GameObject.GetComponent<MeshRenderer>();
with
MeshRenderer showZone = gameobject.GetComponent<MeshRenderer>();
I think this will do,
Also note that in your Error statement ,it is saying that GameObject is a Class or a data type not an object
Do you see the gears? Yes? Click it and click remove component.
You can use the declaration:
other.gameobject.GetComponent< MeshRenderer>().Setactive (false);
One Line Reference...after the condition is fulfilled..
For more precise help regarding MeshRender, see the Unity Documentations..
I had same issue with my declaration and i just fixed it by changing "G" to "g" in gameObject and i declared it in Start so it is like...
MeshRenderer showZone = gameObject.GetComponent();

Categories