- Home /
Modifying a variable of another script with get set
I have this nasty line of code in my program, which interacts with a get set function of another gameobject's script.
It looks likes this:
GameObject.Find("Plane").GetComponent<PlaneScript>().SetPlaneNum = number;
It works, but is there a cleaner / better way to do this?
I appreciate any help!
You may code out a public variable (of type GameObject), where you assign the GameObject in the editor. It depends on the game wich style is better.
What maspi said, or at least cache the PlaneScript reference in a private variable if this setting is changed often. Object lookups are a bit expensive.
Thanks guys! I did try to store the path in a variable like you suggested, but I just wasn't sure what type of variable I would use for a script path. I found I could do PlaneScript planeScript = GameObject.Find("Plane").GetComponent()
It works, and I believe this is what you meant by cache. If you could, let me know if this is proper. Thanks again!
Your answer
Follow this Question
Related Questions
Getting errors when referencing a variable. 1 Answer
GetComponent error 2 Answers
A generic "Find GameObject From Path" method. 2 Answers
Only change a variable on the instaniated object not the prefab. 0 Answers
fix script to get C# var from another object instead of object this script is attached to 2 Answers