I never really use Get Component so I’m pretty terrible at using it so I need a bit of help here. I’m trying to set a str in one script to a string from another script. Here is my code.
GameObject sphereThing = GameObject.Find("Sphere");
main mainScript = sphereThing.GetComponent();
//So now obviously if I wanted to get the string I would do
capitalName = mainScript.civName;
However, this doesn’t work. What did I mess up?
The script is on a gameobect called Sphere (Because I forgot to rename it and now I already have so much done I dont want to change it… so laziness, but there are no other gameobjects in the hierarchy called sphere so it’s all good, for now. )
main mainScript = sphereThing.GetComponent();
//it should be like this
main mainScript = sphereThing.GetComponent<thecomponentyourlookingfor>();
also put gameobject.find in awake
this is a litle example
public GameObject someobject;
public GameObject somescript;
;
//put it in awake
someobject= gameobject.find ("SOMETHING");
//then
somescript= someobject.GetComponent<srciptyourlookingfor>();
UnityEngine.GameObject does not contain a definition for find?
//Edit
Actually I just screwed around a little and got it to work. Putting it in Awake seemed to be the magic touch, thanks.