Basic Unity: Can someone explain the referencing and instantiation process?
So I use C# to script in Unity and I have a background in basic C pointer logic and Java if that helps in parallelism when explaining.
I am not exactly sure how to go about spawning objects and have them work in conjunction with other spawned objects by referencing objects between each other.
This problem can be better illustrated with an example. So I want to create a new object ( called "player") from an already existing one named that is called "GameMaster".
So in GameMaster I have written:
public GameObject player;
....
GameObject playerInstance =instantiate(player, new Vector3(0,0,0), Quaternion.identity);
playerInstance.GetComponent<ScriptName>().SetArguments(argument1, this.GameObject);
So to spawn a certain Object Type I have to drag and drop on the editor as I had set it to public (seems to be the standard, and would love to know a better way if there is). And to set arguments I have to get the script component from the object and call the method and give the needed variable values.
Now, the given argument "this.GameObject" does this reference the current instance of the GameMaster object or does this pass a copy of the current object and put it in the "GameObject gameMaster" variable in "player".
In other words, is it correct to say; when I use a GameObject variable in Instance(....) I just want an instance of this class with the certain values already set but in the earlier scenario in setArguments() I am giving a pointer reference of the GameMaster which's values will reflect those of the original GameMaster if I call a getter kind of like in Java when I pass an object between classes?
Answer by tormentoarmagedoom · May 10, 2018 at 02:50 PM
Good day.
I don't know what you know and not, in some things it seems you are little lost, i will explain few things:
the word "this" means "this script" (not this class, not the object containing the script)
Gameobject (with capital G) is the class type.
gameObject is the GameObject that contains this (you see i used "this" !)
Now, to reference a object, you can do it via scripting or via inspector. If a variable is "public", like this:
public GameObject NiceObject;
You will be able to drag a GameObject from the scene to the script inspector. If the variable is for example a string
public string NiceWord;
You will be able to assign the string from the inspector.
But! for instantiating (create clones of something), maybe you want to instatiate an object that is not yet in the scene, thats where prefabs appear.
If you have a GameObject in the scene and drag it into any Assets folder, it will be stored as a prefab. You can drag any prefab to a public variable of the script.
Sooo
You can Instantiate a prefab (imagine i stored a object called UglyMan as a prefab, and dragged the prefab into NiceObject variable). If i do this, a UglyMan will be instantiated.
Instantiate (NiceObject, position, rotation);
But, if ineed to change things from this instantiated object, i better to do this:
GameObject ObjectInstantiated = Instantiate (NiceObject, position, rotation);
So now, i can modify it as any other gameobject
ObjectInstantiated. transform.position = ....
Destroy (ObjectInstantiated.GetComponent<RigidBody>());
....
Another thing, is reference objects via script (not by editor), so variables don't need to be public. We could say to find the prefab:
NiceObject = Resources.Load("UglyMan") as GameObject;
or a gameobject from the scene
NiceObject = GameObject.Find("UglyMan");
or by its tag (of course you need to assign that tag at the boject inspector)
NiceObject = GameObject.FindObjectWithTag("BliBlaBlu");
I'm not sure to helped you :D If want something ,pelase do a specific question :D
BOUM! my hands are fire! Bye :D
Thank you! Sorry for my lack of clarity. But lets say in my example.when I pass in a script argument to another script (this.GameObject) do I pass the pointer (like the direct address to it) or do I pass a copy of it?
Your answer
Follow this Question
Related Questions
Instantiate a script into an instantiated prefab 3 Answers
Stuttering in build 0 Answers
Network.Instantiate passing parametrs to instantiated object, unassigned reference exception 0 Answers
Instantiate after loading scene 0 Answers
Unity job is failed error. 0 Answers