Referencing an GameObject

Updated on December 23, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on December 21, 2017

Hello!

I am pretty new to coding in c# with unity. I was wondering how you reference an GameObject varaiable from one script to another? I made the variable public but the other script cant find it.

 

Thanks

  • Liked by
Reply
0 on December 22, 2017

i am pretty new too but you could use player prefs for small amount of datas

 

 

  • Liked by
Reply
Cancel
0 on December 23, 2017

To get a reference to another script on another gameobject you create a tag on the gameobject you wish to find the script attached to then use 

ScriptName scriptName = GameObject.FindGameObjectWithTag("Tag Name").GetComponent<ScriptName>();

 

ScriptName is the name of your script you are trying to access and Tag Name is the name of the tag the gameobject the script is attached to has.

 

Or Alternatively if you only have one instance of the script in your scene you could use

ScriptName scriptName = FindObjectOfType<ScriptName>();
  • Liked by
Reply
Cancel