GameObject.Find : why not working?
Hello everyone, maybe it will look like a stupid question to all of you, but until now I only programmed in darkbasic and I'm just starting with Unity, so please, make me understand why this code doesn't work. I want to have access to an object (child so it moves together) so I can use it's collision as a trigger. In the code it only rotates with Z because I was trying to see when the access worked, but it didn't, it continued saying ""NullReferenceException: Object reference not set to an instance of an object playerBehaviour.Update () (at Assets/Scripts/playerBehaviour.js:44)" I read and re-read the pages on the other object access and the GameObject.Find(), but I couldn't understand what I did wrong, neither figure it from the message. Please help, I'm used to a syntax much simpler, so it may look easy but it isn't for me. Code not working:
#pragma strict
public var giocatore : Rigidbody;
private var velocita : float = 1;
private var collinf : GameObject;
function Start () {
giocatore = GetComponent.<Rigidbody>();
collinf = GameObject.Find ("Giocatore/CollisoreInferiore") ;
}
function Update () {
if ( Input.GetKey (KeyCode.LeftShift) ) {
velocita = 2;
}
else {
velocita = 1;
}
if (Input.GetKeyDown (KeyCode.Space) ) {
giocatore.velocity = Vector3 (0, 5, 0);
}
if ( Input.GetKey (KeyCode.W) ) {
giocatore.transform.Translate (0, 0, velocita*Time.deltaTime);
}
if ( Input.GetKey (KeyCode.S) ) {
giocatore.transform.Translate (0, 0, -velocita*Time.deltaTime);
}
if ( Input.GetKey (KeyCode.D) ) {
giocatore.transform.Translate (Time.deltaTime, 0, 0);
}
if ( Input.GetKey (KeyCode.A) ) {
giocatore.transform.Translate (-Time.deltaTime, 0, 0);
}
if ( Input.GetAxis ("Mouse X") > 0 ) {
giocatore.transform.Rotate (0, 180*Time.deltaTime, 0);
}
if ( Input.GetAxis ("Mouse X") < 0 ) {
giocatore.transform.Rotate (0, -180*Time.deltaTime, 0);
}
if ( Input.GetKey (KeyCode.Z) ) {
collinf.transform.Rotate (0, 0, 180*Time.deltaTime);
}
}
Try
collinf = GameObject.Find ("Giocatore").transform.Find("CollisoreInferiore").gameObject;
Or better still, make collinf a Transform ins$$anonymous$$d of a gameobject and use:
collinf = GameObject.Find ("Giocatore").transform.Find("CollisoreInferiore");
meat5000 thank you for your reply, but trying the first way it makes the same error, the second way it says this: "NullReferenceException: Object reference not set to an instance of an object playerBehaviour.Update () (at Assets/Scripts/playerBehaviour.js:44)"
in Java I never had to add the parent to the object i am looking for ( that's what it looks like in your script). I just do collinf = GameObject.Find ("CollisoreInferiore").Is collisoreinferiore a component or a child or what
Answer by Numisi · Aug 20, 2016 at 10:04 PM
Now it is a child. I tried to use the GameObject.Find in either way: making it a child and separating it (obviously I changed the "Giocatore/CollisoreInferiore" stuff when it wasn't a child). I made it a child so it moves toghether, I should use it as a proximity trigger in a specific position. Today I tried to use the find with tag command and it worked perfectly at the first try. This only makes me more confused about why this doesn't work.
well maybe because it is a java script. I toiled for hours trying to figure out why the changes I made weren't doing anything...then suddenly it kicked in after I went on to something else for a while
Answer by 1573680378 · Mar 14, 2017 at 05:07 PM
Maybe you should try collinf = GameObject.Find ("/Giocatore") lt's working for me. like chinese 玄学