Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error: NullReferenceException: Object reference not set to an instance of an object. C#

Discussion in 'Scripting' started by n-carlson1, Jul 31, 2015.

  1. n-carlson1

    n-carlson1

    Joined:
    Jul 11, 2015
    Posts:
    5
    I'm trying to take away player health when an object touches the destroyer at the bottom of the screen. The problem that arises though is that I'm trying to call the damage function from a different script and I believe I did it incorrectly. In the picture is an example of what's happening.

    http://i.imgur.com/zPyeZM9.png[1]

    The method I have in the destroy script

    Code (CSharp):
    1.  
    2. void OnCollisionEnter2D(Collision2D col){
    3.     if(col.gameObject.tag == "BadGuys") {
    4.         carController carDam = col.gameObject.GetComponent<carController>();
    5.         carDam.DamagePlayer(1);
    6.         Destroy (gameObject);
    7.     }
    8. }
    9.  
    This is DamagePlayer method from my carController script

    Code (CSharp):
    1.  
    2. public void DamagePlayer (int damage){
    3.     stats.curHealth -= damage;
    4.     if (stats.curHealth <= 0) {
    5.         Destroy (gameObject);
    6.         ui.gameOverActivated ();
    7.     }
    8. }
    9.  
    10.  
    I added in the two lines for carcontroller and carDam to try and call a different script so it could take the damage if a car reaches the destroyer. So basically I'd like to have the player lose health each time an "BadGuys" object collides with the destroyer at the bottom of the screen. Without the two lines, the destroyer works properly, but when I add those lines, it suddenly just gets "caught".
    Also, I can't add the carController component to the badguys because it has more methods in that script that can't be applied.
     
  2. ande04b

    ande04b

    Joined:
    Aug 29, 2012
    Posts:
    119
    Personally, I would probably put the destroy-script on the destroyer object, and then check for collision with other objects and destroy them if the are tagged as "enemy" or whatever.

    Which object currently holds the script that handles the destroy mechanism?
     
  3. n-carlson1

    n-carlson1

    Joined:
    Jul 11, 2015
    Posts:
    5

    The script that currently handles the destroy mechanism is the "Destroyer" script. I was referencing the damage taken from a separate script called carcontroller which has a method written for taking damage for hitting some objects, and also has a method for moving the car.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Post the entire error and say which line, specifically, it's saying is causing a null reference exception.
     
  5. n-carlson1

    n-carlson1

    Joined:
    Jul 11, 2015
    Posts:
    5
    Well I fixed the error message, so to speak. The problem is it's still not working. I attached the carController script to the player after that, which is kind of shotty now because it has the movement and health of the player.Though when a bad guy hits the destroyer and dies, it doesn't take away health from the player, it takes it from the destroyer, and I want it to take health away from the player. I'm all sorts of confused now with how far I'm in it.


    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D col){
    2. if(col.gameObject.tag == "BadGuys") {
    3.     carController carDam = col.gameObject.GetComponent<carController>();
    4.     if(carDam != null){
    5.     carDam.DamagePlayer(1);
    6.     Destroy (gameObject);
    7.     }
    8.   }
    9. }

    I don't know how to just reference the one damage aspect from the carController without getting everything else, and do I have to add that script to the component for the destroyer?
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    In a collision detection, "col" refers to the object that collided with the current object (the one this script is on).

    If this script is on the destroyer, then "col" refers to the cars that are coming down and slamming into it. You check if the "col" objects are tagged "BadGuys", and if they are, then you pull a component from them called "carController". You then run a function on that "carController" script called "DamagePlayer" and include the amount of damage to be done. Then this object (the destroyer) destroys itself.

    In the DamagePlayer script, if the script is placed on the "BadGuys" objects, then it'll call a script you've referenced called stats and reduce the curHealth stat. There's no indication from the script about whether "stats" refers to a script on the current gameObject (the "BadGuys") or the player, so I have no way of helping with that. The script reduces the health on the "stats" script it's referencing, then destroys its own GameObject.

    Worth noting that the "ui.GameOver blah blah blah" probably won't get called, because you just destroyed this script along with this script's GameObject. That's how destruction works.
     
  7. Rokkimies

    Rokkimies

    Joined:
    May 18, 2015
    Posts:
    12
    Might be a long shot, but check if the interacting scripts are both in the same namespace.
    by: a very long day of "why this doesn't work?!"