- Home /
Converting GameObject into Transform?
Hey everyone! i have a small error and i am hoping the title is right for my problem, but sorry if it isn't.
I have a game in which the user can click on the floor and a node will appear on the floor where the mouse is, and if they click anywhere else the node will move to that location. This code works perfectly.
I also have another code in which uses the Nodes as way points in which my tank will follow when its clicked. I have the click code working, but i am trying to get the way point code to search the Game world for the Node in which i Instantiated with the click code. I can make it find it using Game Objects but the code requires it to be a transform variable. Please help me my way point code is,
var waypoint : Transform;
var speed : float = 20;
static var pointput = false;
function Update ()
{
if (Waypoints.pointput == true)
{
waypoint = transform.Find("Tank_Waypoint(Clone)");//i am guessing this is the problem?
if (Unit_Press.selectionhudon == true)
{
var target : Vector3 = waypoint.position;//This is the line with the error
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1)
{
velocity = Vector3.zero;
}
else
{
velocity = moveDirection.normalized * speed;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
}
When run i click on my tank, and it puts the selection box around it, then when i right click on the floor it places the Node but shows an error when the tank try's to follow it. The waypoint code does work when the nodes are already in the scene but i am trying to make it so i have to place the nodes. The error is,
NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19) Waypoints.Update () (at Assets/Scripts/Waypoints.js:12)
please help me! thank you very much to who ever does
Answer by dannyskim · Dec 08, 2011 at 11:55 PM
Most likely the error is actually coming from your transform.Find:
waypoint = transform.Find("Tank_Waypoint(Clone)");
No exception is thrown if it returns null, that's why it errors out at the line that you commented after where the debugger is reporting the issue.
I'm not quite sure if this is true, but the Unity documentation for transform.Find states that it looks for objects that are children of the object that you are calling the function from:
http://unity3d.com/support/documentation/ScriptReference/Transform.Find.html
You may want to try changing that line of code to:
waypoint = GameObject.Find( "Tank_Waypoint(Clone)" );
and changing the corresponding line of code to:
var target : Vector3 = waypoint.transform.position;
and changing your variable declaration at the top to:
var waypoint : GameObject;
ahah! thank you my friend! it works :D i wish i could rate twice for the speed of your reply!
How would you make it ins$$anonymous$$d of finding the object by name but by tag?
waypoint = GameObject.Find( "Tank_Waypoint(Clone)" );
GameObject.FindGameobjectWithTag("some tag");
take a look in the autocompletion possibilities before asking :)
Thanks :) I will take your tip on board. Sorry for any inconvenience