1
using UnityEngine;

using System.Collections;

public class GameHandler : MonoBehaviour {

public GameObject playerPrefab;
public GameObject playerCameraPrefab;
public GameObject player;
public GameObject playerCamera;
// Use this for initialization
void Awake () {
    player = Instantiate(playerPrefab,new Vector3(5,1,5),Quaternion.identity) as GameObject;
    playerCamera = Instantiate(playerCameraPrefab,Vector3.up,Quaternion.identity) as GameObject;
}

// Update is called once per frame
void Update () {

}

}

using UnityEngine;

using System.Collections; using System.Collections.Generic;

public class EarthMagic : MonoBehaviour {

public GameHandler gameHandler;

public GameObject EarthMagicWall;
public PlayerMovement PM;
public UIControls UI;
public Transform spinner;

public List<Transform> circleStones = new List<Transform>();

// Use this for initialization
void Start () {
    //connect to the gameHandler
    gameHandler = GameObject.Find ("WorldHandler").GetComponent("GameHandler") as GameHandler;
    PM = transform.GetComponent("PlayerMovement") as PlayerMovement;
    UI = transform.GetComponent("UIControls") as UIControls;
}

// Update is called once per frame
void Update () {
    if(UI.gamePaused == false) {
        if(Input.GetButtonDown("Channel")) {
            if(PM.isGrounded == true) {
                Vector3 checkPosition = transform.position + (transform.forward * 3);

            //  Debug.DrawRay(transform.position, checkPosition, Color.blue, 20);
            //  Debug.DrawRay(checkPosition, transform.TransformDirection (Vector3.down), Color.red, 20);

                RaycastHit hit;
                if (Physics.Raycast(checkPosition, transform.TransformDirection (Vector3.down), out hit, 5)) {
                    if(hit.collider.name == "Terrain") {
                        Instantiate(EarthMagicWall, hit.point, transform.rotation);
                    }
                }
            }
        }

        if(Input.GetMouseButtonDown(0)) {
            if(circleStones.Count > 0) {
                Transform temp = circleStones[0].transform;
                circleStones.RemoveAt(0);
                temp.parent = null;
                temp.rigidbody.AddForce(Camera.main.transform.up * 20);
            }
        }
        ControlRocks ();
    }
}

void ControlRocks() {
    foreach(Transform circleStone in circleStones) {
        switch(circleStones.Count) {
        case 1: case 2:
            if(circleStones.IndexOf(circleStone) == 1) {
                circleStone.transform.localPosition = new Vector3(0,0, 2); 
            } else {
                circleStone.transform.localPosition = new Vector3(0,0, -2);
            }
            break;
        case 3:
            if(circleStones.IndexOf(circleStone) == 1) {
                circleStone.transform.localPosition = new Vector3(0,0, 2); 
            } else if(circleStones.IndexOf(circleStone) == 2) {
                circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, (Mathf.Sqrt(2) * -1));
            } else {
                circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, (Mathf.Sqrt(2) * -1));
            }
            break;
        case 4:
            if(circleStones.IndexOf(circleStone) == 1) {
                circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, Mathf.Sqrt(2));  
            } else if(circleStones.IndexOf(circleStone) == 2) {
                circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, (Mathf.Sqrt(2) * -1));
            } else if(circleStones.IndexOf(circleStone) == 3){
                circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, (Mathf.Sqrt(2) * -1));
            } else {
                circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, Mathf.Sqrt(2));
            }
            break;
        case 5:
            if(circleStones.IndexOf(circleStone) == 1) {
                circleStone.transform.localPosition = new Vector3(0,0, 2); 
            } else if(circleStones.IndexOf(circleStone) == 2) {
                circleStone.transform.localPosition = new Vector3(1.9f,0, 0.62f);
            } else if(circleStones.IndexOf(circleStone) == 3){
                circleStone.transform.localPosition = new Vector3(1.18f,0, -1.61f);
            } else if(circleStones.IndexOf(circleStone) == 4){
                circleStone.transform.localPosition = new Vector3(-1.18f,0, -1.61f);
            } else {
                circleStone.transform.localPosition = new Vector3(-1.9f,0, 0.62f);
            }
            break;
        }

        Stone stone = circleStone.gameObject.GetComponent("Stone") as Stone;
        if(!stone.isRigid) {
            Rigidbody shardRigidBody = stone.gameObject.AddComponent<Rigidbody>();
            shardRigidBody.mass = 5;
            stone.isRigid = true;
        }
    }
}

}

So the error above occurs at line 63 in EarthMagic. I've solved this type of error before but I don't really understand it this time. This code was working before and I did not change that entire function. I did however start to instantiate my player and camera rather than simply place them in the editor. I'm sure this is the cause of the problem but I don't really know how to fix this. Any ideas?

4
  • which one is line 63? ;) Commented Jun 1, 2014 at 4:07
  • You have public variables that will need to have GameObjects dragged into their positions so that they know what they are referencing… Make sure you have fixed up those boxes and post back with your results… If you've already done that then we'll need some more information from you Commented Jun 1, 2014 at 4:37
  • Yes I have done that Savlon. What do you need to know? Line 63 is circleStone.transform.localPosition = new Vector3(0,0, -2); Commented Jun 1, 2014 at 5:12
  • possible duplicate of What is a NullReferenceException and how do I fix it? Commented Feb 16, 2015 at 10:44

1 Answer 1

0

You have a null element in your list. It is a lot of code and you should debug it yourself, it will help you.

For now you can implement this simple Hack.

foreach(Transform circleStone in circleStones) 
{
    if(circleStone == null)
       continue;       

    switch(circleStones.Count) 
    {
     . 
     .
     .
    }
 }

This code will check for null, if found then it will just skip that element.

Sign up to request clarification or add additional context in comments.

2 Comments

Weird. That fixed most of it and I managed to fix the rest myself, but why didn't I need that before? And I was trying to debug it but got stuck. So I came here for help. Anyway, thanks. This was really bothering me.
You are doing something above in the code that makes one or more elements null. Good luck on your game!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.