- Home /
Instantiate JS error that i can't figure out
Hello community, I do hope that this question is not too redundant, I have been looking through several questions and I don't see anything that answers my question.
I am trying to mimic an Instantiate/swap/destroy GameObject example from Will Goldstone's tutorial book on Unity. My goal: upon OnTriggerEnter (), and pressing Fire1, I want to have one gameObject swapped out for another in a different place on the X-axis. There is an example of this in the book. I am using the same code, but I get an error message that I can not get the answer for. The error that i get for this line:"var kickedBall : GameObject = transform.Find("firstball").gameObject;" is this: NullReferenceException samplecollide.Update () (at Assets/Scripts/samplecollide.js:9)
 #pragma strict
 
 
 static var shoot : boolean = false;
 public var secondball : GameObject;
 
 function Update () {
     if (Input.GetButtonDown("Fire1") &&shoot==true) {
     var kickedBall : GameObject = transform.Find("firstball").gameObject;
     kickedBall.transform.Translate(-1,0,0);
     Instantiate(secondball, transform.position, transform.rotation);
 
     Destroy(gameObject.Find("kickedBall")); 
     }
 }
 
 function OnTriggerEnter(col : Collider) {
 
 
 if(col.gameObject.tag =="Player"){
 
     Debug.Log("found");
     shoot = true;
     
     
     }
 }
I have attached this script to a gameObject called firstball which has a sphere collider that Is Trigger. I just don't know what is wrong with my 'kickedball' variable line. Any help would be greatly appreciated.
Answer by Montraydavis · Nov 23, 2012 at 10:31 PM
 kickedball = Instantiate ( Obj, position, rotation ) ;
Try that instead. Not really sure why you used transform.Find("blaa"). . ..
This is very good $$anonymous$$ontray! Thank you so much for your time...that worked.-this fixed the first part of my issue. I was following a will goldstone tutorial, and this is how he suggested to do the swap. I have been toying with this for 5 days before i remembered this forum. Thanks a million -- now i will try to figure the second part. Hopefully, on my own, but I am new to this so it's a lot of work for me. Cheers.
You are very welcome! If you don't figure it out, feel free to send me a message, or post the question.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                