0

after trying to acces a variable in another Script of another GameObject out of a List, I get every time an Exception. The Main code looks like this:

private var BombList = new List.<GameObject>();
private var BombTemp : GameObject;
private var BombTempScript : Bomb;
function Start () {
    BombTemp = null;
    BombTempScript = null;
    BombList.Clear();
}
function Update () {
if(BombList.Count > 0){
        for(var i : int = 0; i<BombList.Count;i++){
            BombTemp = BombList[i];
            BombTempScript = BombTemp.GetComponent.<Bomb>();
            if(BombTempScript.bombCountdown <= 0){
                BombTempScript.explode();
                BombList.Remove(BombTemp);
                addHealth(-1);
            }
        }
    }
}

function OnTriggerEnter (other : Collider) {
if(other.gameObject.CompareTag("Bomb")){
        BombList.Add(other.gameObject);
        other.gameObject.GetComponent.<Bomb>().notListed = false;
    }
}
function OnTriggerExit(other : Collider){
    if(other.gameObject.CompareTag("Bomb")){
        if(BombList.Contains(other.gameObject)){
            BombList.Remove(other.gameObject);
            other.gameObject.GetComponent.<Bomb>().notListed = true;
        }
    }
}

If there isn't an object in the List the Code in the Update function does not work as intended. But when there is an object inside it produces a NullReferenceException in the if Line:

if(BombTempScript.bombCountdown <= 0)

The variable which is pointed at named bombCountdown, is continuously changing. Here is the intended code:

#pragma strict
public var bombCountdown : float;
public var notListed : System.Boolean;

function Start () {
    bombCountdown = 10.0;
    notListed = true;
}

function Update () {
    bombCountdown -= Time.deltaTime;
    if(bombCountdown <= 0 && notListed)
        explode();
}
function explode(){
    Destroy(myText);
    Destroy(this.gameObject);
}

I hope you could help us.

Thanks in advance, the Silly Scientists

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 1
    It seems to me like `BombTemp.GetComponent.()` is returning null - ie. There is no `Bomb` component directly on the GameObject `BombTemp`. Can you use logging/check the inspector to determine what variable is actually throwing the exception? – Serlite Aug 10 '17 at 20:33
  • Also, why are `BombTempScript` and `BombTemp` class level properties? They should be local scope (and lower case, eg: `bombTempScript`) – Draco18s no longer trusts SE Aug 10 '17 at 21:17
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hellium Aug 10 '17 at 21:22
  • I just looked over it and the result was there is a problem with the BombTemp.GetComponent.(); After commenting the for loop I had the same issue with: other.gameObject.GetComponent.().notListed = false; The List works fine. Finding the Bomb shouldn't be the Problem as the function gives out the Object @Hellium nah it isnt a duplicate. I know whats the Exception means but I don't now it's origin. – Silly Scientists Aug 10 '17 at 21:38

2 Answers2

0

I think there's a small bug in the code, which is making it happen.
In the Bomb script, in the following portion, you are destroying the bomb object, without removing it from the BombList of the first script.
enter image description here

As a result, BombList[i] may become null, as it may already have been destroyed.
enter image description here

If you make sure that you update the BombList when the Bomb is destroyed, I guess the code will work.

ZayedUpal
  • 1,603
  • 11
  • 12
0

You remove gameobject from list in the for loop, so all indexes in the list shift to left from removed object. And the count is not the same as for loop started. At first you must complete your for loop, then remove objects after that.

BlackMB
  • 230
  • 6
  • 20