Search Unity

Accessing Variables for a Script attached to a just-Instantiated object.

Discussion in 'Scripting' started by QuinnDP, Feb 1, 2017.

  1. QuinnDP

    QuinnDP

    Joined:
    Dec 12, 2016
    Posts:
    22
    Making a space-invader style game (fairly early days of learning C# here) I have both the player and the enemies shooting the same prefab projectile. This projectile has a load of variables that change which sprite model the projectile uses. The prefab is set by default to the correct size, shape and colour for the player, but the enemies need to be able to change these variables of their projectiles each time they spawn. Because of the way both the player and the enemy spawn projectiles, I cannot make these static variables, for then both the players projectiles would also change colour. How can gain access to a script for the object you just instantiated, when other objects in the scene share the same name and script?

    Example scripts below for PlayerController and Bullet script:

    Code (CSharp):
    1. public class PlayerController: MonoBehaviour {
    2.  
    3.     GameObject projectile;
    4.     public float fireRate = 0.2f;
    5.     public float projectileSpeed = 6f;
    6.  
    7.     void Start() {
    8.         projectile = Resources.Load<GameObject>("Prefabs/Projectile");
    9.     }
    10.    
    11.     void Update() {
    12.         if (Input.GetKeyDown(KeyCode.Space)) { InvokeRepeating("Fire", 0.00001f, fireRate); }
    13.         if (Input.GetKeyUp(KeyCode.Space)) { CancelInvoke("Fire"); }
    14.     }
    15.  
    16.     void Fire() {
    17.         GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
    18.         bullet.name = projectile.name;
    19.         bullet.rigidbody2D.velocity = new Vector3(0, projectileSpeed);
    20.     }
    21. }
    22.  
    Code (CSharp):
    1. public class Bullet : MonoBehaviour {
    2.  
    3.     public string colour = "Blue";
    4.     public float damage = 100f;
    5.  
    6. }
    TL : DR version : The bullet script is attached to the projectile prefab, but how can I use the PlayerController script to access the variables inside this instance of the Bullet script without making them static variables?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You're already doing it technically in your Fire script.
    If you have lots of bullets, you'll have to add them to a list. Then if you wanted to do something to all bullets, you have to loop through the list doing a getComponent call on them

    Code (CSharp):
    1. foreach(GameObject bullet in bullets)
    2. {
    3.    bullet.GetComponent<bulletScript>().changeSpeed(); //Just an example of accessing the changeSpeed method on a bulletScript script.
    4. }
    Otherwise, if you're just changing it after you instantiate, you can just do that in your fire script to each object.
     
    QuinnDP likes this.
  3. QuinnDP

    QuinnDP

    Joined:
    Dec 12, 2016
    Posts:
    22
    The following worked, thanks! I just couldn't find the syntax for grabbing the script itself :p

    bullet.GetComponent<Bullet>().colour = "Red";

    Most people always suggest using a public method like changeSpeed() and changeColour() , is there any reason to place a method into the Bullet Script when you could just make the variable public and change it this way?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    The normal reason you would do a method over a public field is you might need to do more in a method. For this case, you're fine. But think of it this way, if you wanted to limit the color to red, for example, you can't do that in a public field. In a method, you could check what color they were trying to set it to and make sure it isn't set to yellow or green or something.

    That's a pretty simple example. Usually you think of this with like...banking. A person who can withdrawl money by just taking straight from their balance vs a method that will check if the amount they want to withdraw is actually available for them to take.