I have a game where you instantiate troops with waypoints to move to another area.
The troops have a waypoint script attached to them.And I want to access the waypoint script target variable so I can set the waypoint to where I clicked.
I have set up the clicking to find which gameObject I clicked (raycast hit) but how will I set up a way to access the target variable (a Transform) to the clicked target?
By the way, I read the documentation on GetComponent but I don't know how to get the variable and how to set it (is it just like regular once you get the variable?).
Thanks in Advance. (Please attach script to your answer :P)
Note: I want to set the target variable to where I clicked only for that unit so instantiating waypoints won't work because all units will go for that point.
in java script just write
in C# you should use one of these two methods:
GetComponent<yourScriptName>().target = clickedTransform;
passing the type of the script to general function
(GetComponent(typeof(yourscriptname)) as yourscriptname).target=clickedTransform;
you can cache the result of GetComponent to use it later.
create a variable of type yourscript and in Start use GetComponent to set it to the component's reference and then use that variable instead of calling GetComponent in each click event. it's much faster in devices like the iphone.
var w : WayPointScriptName;
function Awake ()
w = GetComponent(WayPointScriptName);
now you can just use w.target anywhere you want.
var newTroop : GameObject = Instantiate( troopObject );
newTroop.GetComponent(WayPointScript).target = clickedTransform;
Then newTroop will be set to your newly instantiated troop object.