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

Need help with Game Manager and multiple different objects

Discussion in 'Scripting' started by ChrisX, Mar 11, 2015.

  1. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Hello all, I'd like to ask about a certain thing on Game Managers...

    The situation is like this: Already in the scene are 3 objects:
    1. The Player
    2. The Game Manager
    3. The Enemy Spawner, which will instantiate objects called The Enemy.

    The Player has this to link itself with the existing Game Manager:

    Code (CSharp):
    1. public GameObject gm;
    Whereas in Game Manager, there is this variable that can be manipulated via 'SetX'

    Code (CSharp):
    1. void SetX(float a){
    2. x = a;
    3. };
    The Player can access this easily. Now the problem is, I want The Enemy, spawned by the Enemy Spawner, able to access THIS Game Manager. I already made the prefab of the Game Manager and linked it to The Enemy prefab. But when the enemy is actually spawned, it's not responding to the Game Manager that has already been on the scene, like if I manipulated from the player that the X is now 100, then other numbers, the Game Master object on the Enemy is still the same as before!

    A little help here? Thanks!
     
    Last edited: Mar 11, 2015
  2. Niekos

    Niekos

    Joined:
    Mar 11, 2015
    Posts:
    8
    If you just linked the Prefab it won't work, since when you instantiate an object onto the scene, you are acctually making a clone of that prefab. so make sure you link that CLONE to the game manager if it's manipulating the object in runtime.
     
  3. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    I think the Enemy prefab's connection to the GameManager is in the project
    and not set at runtime, so you could do it in 2 ways :

    1). Store the link to GameManager in the EnemySpawner in the scene.
    After the enemy spawner spawns an enemy, call a method in enemy spawner
    which passes this link to the GameManager to it.

    Quick pseudo code :

    class EnemySpawner {

    public GameObject m_gm; // set this in inspector

    void SpawnEnemy() {

    GameObject enemy = Instantiate();
    enemy.SetGM( m_gm );

    }

    2). In Awake of Enemy script, let it do a Gameobject.Find() for the GameManager in the scene.
     
  4. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Thanks. In the end, I just assigned the command FindWithTag after tagging my GM... worked perfectly. Thanks for the alts though.