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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can you switch references of Gameobject for Scripts during runtime?

Discussion in 'Scripting' started by solider, Jun 11, 2015.

  1. solider

    solider

    Joined:
    May 28, 2015
    Posts:
    9
    So this may be a very obvious question. I wanted to know if it was possible to switch references of Gameobjects for Scripts. For example, for my Bullet Script, I added the gameObject "bullet" to the Script, so that my Script can instantiate the bullet. But I want to switch this referenced gameobject from "bullet" to another GameObject called "laser" , so that my Script will instantiate the laser gameobject instead of the bullet gameobject. I know how to do it manually, but is there a way to switch it during gameplay. I am trying to change this reference when my player touches a power-up symbol. So I am using OnTriggerEnter2D function. I tried to use FindGameObjectwithTag("laser"), but I end up getting an error. Any help would be great.
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Keep a reference to both in the inspector. Then just have the method which spawns the bullet have a parameter as the method declaration like:

    Code (CSharp):
    1. void SpawnProjectile (GameObject prefabGameobject)
    2. {
    3. // do stuff
    4. GameObject newProjectile = GameObject.Instantiate (prefabGameobject);
    5. newProjectile.GetComponent <SomeScript>().DoSomething ();
    6. }
    then when you go to spawn one or the other, hand it the prefab you need to spawn like this:

    Code (CSharp):
    1. SpawnProjectile(LaserPrefab);
    or:

    Code (CSharp):
    1. SpawnProjectile(BulletPrefab);
    Good luck!
     
    Last edited: Jun 11, 2015
  3. solider

    solider

    Joined:
    May 28, 2015
    Posts:
    9
    I am using Invoke Repeating to call SpawnProjectile(), so that means I cannot take in an argument. Is there another way to get around this.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    Don't use InvokeRepeating or Invoke, they're bad.

    Replace them with coroutines that allow you to use the methods like real methods, instead of landing you in reflection nightmares.

    Or, if you really insist, have the method you're invoking select the gameobject to spawn from a field:

    Code (CSharp):
    1. public GameObject bulletPrefab;
    2. public GameObject laserPrefab;
    3. private GameObject usedProjectile;
    4.  
    5. void Start() {
    6.     usedProjectile = bulletPrefab;
    7.     InvokeRepeating("Fire", 0, 2f);
    8. }
    9.  
    10. void Fire() {
    11.     Instantiate(usedProjectile);
    12.     //move the projectile somehow
    13. }
    14.  
    15. void SwitchToLasers() {
    16.     usedProjectile = laserPrefab;
    17. }