- Home /
Respawn Code Null Reference Exception
Hi this is just a small problem Im having, I did have it solved earlier today in class by like an idiot I saved an older version over my fixed code and now cant remember how my teacher fixed it so any help will be much appreciated.
public class Respawn : MonoBehaviour
{
public MarbleControls theMarble; public int respawnTime; public Vector3 floatTime; public GameObject blood; public Vector3 scaleSpeed; public bool invokeToggle; public GameObject thingToFollow; public GameObject destroy; public AudioClip RespawnPlay; public AudioClip ImpactPlay; public AudioClip DeathPlay;
public void OnTriggerEnter(Collider other) {
if (other.tag == "Player")
{
//If fallen then check to see if respawn hasnt been invoked
if (invokeToggle == false)
{
//Invoke respawn and set the toggle to true
Invoke("RespawnMarble", respawnTime);
blood.gameObject.active = true;
Instantiate(blood, other.transform.position, other.transform.rotation);
//Set the toggle to true
invokeToggle = true;
//The Error starts here so the next script "MarbleControls" wont Invoke respawn but the GameObject is deactivated
---destroy.active = false;--- audio.PlayOneShot(ImpactPlay); audio.PlayOneShot(DeathPlay); } } }
public void OnTriggerStay(Collider other) {
theMarble.transform.localScale.Scale(scaleSpeed);
theMarble.rigidbody.AddForce(floatTime);
}
public void RespawnMarble() { //Respawn marble theMarble.RespawnMarble(); blood.active = false; //Marble has been respawned so set the invoke to false invokeToggle = false; audio.PlayOneShot(RespawnPlay); }
}
**MarbleControls Script**
public class MarbleControls : MonoBehaviour
{
public Vector3 spawnPoint;
// Use this for initialization
void Start()
{ rigidbody.maxAngularVelocity = 25; }
// Update is called once per frame void Update() { //Debug.Log("You Pressed W"); if (Input.GetKey(KeyCode.W)) { rigidbody.AddForce(0, 0, -5); } //Debug.Log("You Pressed S"); if (Input.GetKey(KeyCode.S)) { rigidbody.AddForce(0, 0, 5); } //Debug.Log("You Pressed A"); if (Input.GetKey(KeyCode.A)) { rigidbody.AddForce(5, 0, 0); } //Debug.Log("You Pressed D"); if (Input.GetKey(KeyCode.D)) { rigidbody.AddForce(-5, 0, 0); } } // Respawns the marble public void RespawnMarble() {
//This is where the last script is supposed to invoke Respawn but fails because the next line is wrong
---transform.parent.gameObject.active = true;---
rigidbody.MovePosition(spawnPoint);
rigidbody.velocity = new Vector3(0, 0, 0);
rigidbody.angularVelocity = new Vector3(0, 0, 0);
}
}
My teacher changed something on the 2 lines I've tagged, I think it may only be the line in MarbleControls but I cant remember and Im not good enough at programming to figure it out. I can remember the line was quite long but thats about it lol.
Answer by noony · Jun 11, 2010 at 01:12 PM
If you are getting a null reference exception on the line...
transform.parent.gameObject.active = true;
...this would most likely be because the GameObject
that your MarbleControls
script is attached to does not have a parent GameObject
.
Alternatively, if you have not set the public variable destroy
in your first script to refer to anything, that would also result in a null reference exception at the other line you have tagged.
thanks for the quick reply all I had to do was remove parent from the line. I knew it was only a little problem cheers for ya help
Your answer
Follow this Question
Related Questions
Stop Object from deleting HELP ME!!!!!!!!!!!! 1 Answer
Respawn question unanswered 0 Answers
instantiated objects animation making the object reset to 0,0,0?? 1 Answer
Respawn with enemy contact 1 Answer
OnTriggerEnter forcefield help! 1 Answer