- Home /
C# Access Script in Closest Object
Basically, I'm trying to chop down trees using AI. I have multiple objects in the scene with the same name, but the tag "tree". My pathfinding finds the closest object with the tag "tree" and walks over to it. Now, I need to check if the tree he is near has fallen. My logic is to access the script attached to the closest tree and run a function to check this status.
My question is, if I use GameObject.FindGameObjectWithTag("Tree") and assign that to a new GameObject, how can I access the individual script within this GameObject and not all scripts attached to the general tree prefabs?
Answer by Reder13 · Feb 22, 2015 at 06:58 PM
What you are looking for is the command GetComponet say the GameObject tree is your tree you are targeting,
example:
yourScriptNameHere yourScript = tree.GetComponent < yourScriptNameHere> ();
yourScript.aMethod();
this allows you to access a certain script that a GameObject has and call its methods. access its public variables.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to draw a line from the player to the nearest object? 0 Answers
How do I find the closest target with a tag? c# 2 Answers
Distribute terrain in zones 3 Answers