Search Unity

increasing a var in one script from another

Discussion in 'Scripting' started by adamprocter, Apr 2, 2007.

  1. adamprocter

    adamprocter

    Joined:
    Jan 18, 2006
    Posts:
    22
    I want to increase my ammo count when my missles are destoryed
    ammoCount is on the launcher script and destory is on the missle script - so I want to increase ammoCount from my missle script

    I have got a bit lost but I was trying

    Missle.js
    Code (csharp):
    1.  
    2. var addAmmo:Launcher;
    3.  
    4. function OnCollisionEnter(collision : Collision) {
    5. ..../etc
    6. Destroy (gameObject);
    7. addAmmo.ammoCount + 1;
    8.  
    but it complains about NullReferenceException

    Launcher.js
    Code (csharp):
    1.  
    2. public var ammoCount = 4;
    3.  
    4. function Update () {
    5. if (...etc..
    6.  
    7.     instantiatedProjectile.velocity = transform.forward * speed;
    8.     ammoCount --;  
    9.  
     
  2. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    A couple things that might be happening:

    The reference to instantiatedProjectile in your Launcher script might point to null if the instantiatedProjectile has already been destroyed. I can't tell for sure if this would ever happen, though.

    Also, I notice that you're calling Destroy in your Missile script before you add to your ammo count. This means that the ammoCount line won't ever get called.
     
  3. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    The problem is probably that the missile var addAmmo:Launcher; is set to be something in a scene and then the missile is assigned to a prefab and instantiated. This will break that link. In your missile instantiator you need missile.addAmmo = this;

    No. Destroy does not take effect until the next frame.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Change the line in Launcher.js that says

    Code (csharp):
    1.  
    2. public var ammoCount = 4;
    3.  
    to this:

    Code (csharp):
    1.  
    2. static var ammoCount = 4;
    3.  
    And rename Missle.js to Missile.js ;), and change it to this:

    Code (csharp):
    1.  
    2. function OnCollisionEnter(collision : Collision) {
    3. ..../etc
    4. Destroy (gameObject);
    5. Launcher.ammoCount ++;
    6.  
    (Get rid of the "var" line and change the "addAmmo.ammoCount + 1;" line.)

    --Eric
     
  5. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Sorry for leading you astray, Adam. :oops:
     
  6. adamprocter

    adamprocter

    Joined:
    Jan 18, 2006
    Posts:
    22
    worked like a dream
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Although I forgot to say that static variables have the feature of retaining their value from one run to the next. So you probably want to assign the value of ammoCount in a Start function...just declaring it at the top of the script isn't enough.

    --Eric
     
  8. adamprocter

    adamprocter

    Joined:
    Jan 18, 2006
    Posts:
    22
    works even more like a dream :D