Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Passing variables - Javascript

Discussion in 'Scripting' started by Biggidybigz, Feb 5, 2015.

  1. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    Hey everyone. I'm new to programming and I'm struggling here. I've watched multiple tutorials but am unable to understand this concept. I'm using javascript btw.

    1. I want to pass variables from one script to another.

    2. I also want to instantiate a prefab, and be able to pass variables to that prefab

    I have a spaceship, who fires bullets, and those bullets hit enemies.
    Each one of these objects has a script to it (spaceshipscript, bulletscript, enemyscript).

    I want to give the spaceship stats like damage. i want to then be able to pass that "damage" variable to the enemyscipt so that it knows how much health to take off itself. OR possibly pass the damage information onto the bullet, and then from the bullet to the ship (but that seems less efficient).

    Any help? Thanks
     
  2. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    I've tried doing the following on the enemyScript to test it, but it fails -
    var spaceship : spaceshipScript;
    spaceship = GetComponent(spaceshipScript);
    Debug.Log (spaceship.damage);
     
  3. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    1) Passing Variables
    2) From the docs:
    Code (CSharp):
    1. var template : SpaceShip;
    2.  
    3. function Start()
    4. {
    5.   var ss : SpaceShip;
    6.   ss = Instantiate(template);
    7.   ss.Damage = 20;
    8. }
     
  4. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Is there a spaceshipScript attached to the enemyScript gameobject?
    Where is this code executing (Start(), Awake(), OnTriggerEnter()...)?
     
  5. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    spaceshipScript is attached to the Spaceship gameobject. enemyScript is attached to the Enemy gameobject.
     
  6. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    the code "Debug.Log (spaceship.damage);" is being called when the ship gets hit by asteroids (just as a test). I've tested this with Debug.Log ("20") and it works. However when i try to reference spaceship.damage, it doesn't work.

    I get the error "Object reference not set to an instance of an object" btw.
     
  7. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    GetComponent() is only looking at the gameobject it is called from. In this case it's the gameobject your enemyScript is attached to, so it won't find anything not attached to it.

    How are you determining what enemy is being shot at?
     
  8. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    The enemyscript has the function OnTriggerEnter2D(obj : Collider2D) which detects that a specific enemy has been hit by a bullet. I want to get a variable from a script that the enemy gameobject isnt calling. Nobody seems to be able to solve this :S
     
  9. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  10. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Not hard to solve, but the solution depends on how you are doing things.

    obj.gameObject is what collided with the enemyScript.

    So, typically here is how it plays out:
    1) spaceship instantiates something to shoot (or raycasts, but looks like you are using collisions against a bullet object), then either set the damage amount to a variable in the bullet script, or set a shooting spaceship reference to a variable in the bullet script
    2) bullet hits the target
    3) use the variable in the bullet script using obj.gameObject.GetComponent("BulletScript").Damage or similar in the enemyScript to apply damage.
     
  11. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    I want to have all the stats like "damage" on the spaceship, and then pass that variable to the enemy for the purpose of working out how much damage they take from bullets. I need to know how to pass a variable from 1 script to another.

    "spaceship = obj.gameObject.GetComponent("spaceshipScript");" is an improper line of code and errors saying it doesnt know what "obj." is.
     
  12. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    I don't want to find a gameobject, i want to use a variable from 1 script in another script by passing the information across. I tried using your method and it didn't work.
     
  13. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Below is the gist of it. I don't code in UnityScript so there may be some syntax errors, etc., but that's how you do it in general. The SpaceShip instantiates the bullet game object, passes a reference to itself to the bullet script which will be used by the hit enemy script to see how much damage the ship did to it.

    Code (CSharp):
    1. public class SpaceShip extends MonoBehaviour
    2. {
    3.   public var Damage = 20;
    4.   public var BulletTemplate;
    5.  
    6.   public function Fire()
    7.   {
    8.     var bullet = Instantiate(BulletTemplate).getComponent("Bullet");
    9.     bullet.spaceShip = this.gameObject;
    10.     //... whatever else you need to do to the bullet
    11.   }
    12. }
    13.  
    14. public class Bullet extends MonoBehaviour
    15. {
    16.   public var spaceship : GameObject;
    17. }
    18.  
    19. public class Enemy extends MonoBehaviour
    20. {
    21.   public function OnTriggerEnter(other : Collider)
    22.   {
    23.     // ...determine if it's a Bullet that ran into us...
    24.     var damageFromShip = other.GetComponent("SpaceShip").Damage;
    25.     // ...apply damage to enemy...
    26.   }
    27. }
     
  14. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    This won't work because the ship doesn't collide with the enemy. Only the bullet does. And i'm trying to pass a variable from ship to enemy, without including the bullet. I just want to take a variable from one script and pass it to another. Also i don't use classes.
     
  15. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    I've tried this in my enemy script to get the variable of "damage" from spaceshipScript -

    var spaceship : GameObject;
    var damage = 0;
    spaceship = GameObject.Find("spaceship");
    damage = spaceship.GetComponent(spaceshipScript).damage;

    Debug.Log (damage);

    This prints "0" instead of "20" which is the value i've given the damage variable on spaceshipScript. So it's not properly passing this information across
     
  16. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    The code below displays 30 in the debug window for me. Is 'damage' in your spaceshipScript all lowercase?

    enemyScript.js
    Code (CSharp):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5. var spaceship : GameObject;
    6. var damage = 0;
    7. spaceship = GameObject.Find("spaceship");
    8. damage = spaceship.GetComponent(spaceshipScript).Damage;
    9.  
    10. Debug.Log(damage);
    11.  
    12. }
    spaceshipScript.js
    Code (CSharp):
    1. #pragma strict
    2.  
    3. var Damage : int = 30;
    4.  
     
  17. Biggidybigz

    Biggidybigz

    Joined:
    Feb 5, 2015
    Posts:
    10
    AH. Breakthough - it works. Thank you!
     
    Last edited: Feb 6, 2015