Search Unity

Singleton sending GameObjects (ERROR)

Discussion in 'Scripting' started by caioshing1, Sep 25, 2018.

  1. caioshing1

    caioshing1

    Joined:
    Sep 25, 2018
    Posts:
    1
    Hi,

    I created a Singleton to provide to other classes all the GameObjects that I'll use on the game. I created a function that, given the GameObject array index, returns the corresponding GameObjects.
    In the Singleton, in order to initialize the first position of the array (only for test purposes), I tried two different ways:
    1)
    Code (CSharp):
    1. array[0] = GameObject.FindGameObjectWithTag("ObjectOnTable");
    2) changing the array to public and dragging the GameObject manually on engine.

    This is the function:
    Code (CSharp):
    1. public GameObject.GetObject(int index)
    2.     {
    3.         return this.objetos[index];
    4.     }
    In another class, I called an instance of this Singleton and did:
    Code (CSharp):
    1. this.objOnTable = this.bankObjects.GetObject(0);
    However, for these 2 ways, when I execute the game, objOnTable does not receive any value;
    So the question is: how can I assign this GameObjects?

    Thank;
     

    Attached Files:

  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Your entire singleton solution is incorrect. You can't use "new" to create MonoBehaviours, nor can you use constructors, which is most likely making your local bancoObjectors value null. You should resolve all of those issues before you try to debug any other issues.