Search Unity

Unity Multiplayer Referencing Script at runtime

Discussion in 'Connected Games' started by U-GeeN, Jan 23, 2017.

  1. U-GeeN

    U-GeeN

    Joined:
    Mar 22, 2015
    Posts:
    92
    I'm trying to reference my GameMaster script in my player script in the Start() function.
    But I'm getting a "Object not set to an instance" Error.
    Seems like the GameMaster isn't ready for referencing at that point.
    I tried to put it above the default execution time.
    But still no script seem to be able to get a refeernce of GameMaster.

    My solution was to try to reference it in Update like:
    if (gameMaster == null){
    gameMaster = GameObject.Find ("GameMaster").GetComponent<GameMaster> ();
    }
    and it works on the second frame.
    Anyone a better solution?
     
  2. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    615
    You could make a persistent DontDestroyOnLoad gameObject will all persistent game controlling scripts then use a singleton design pattern.

    Code (csharp):
    1.  
    2. public static GameMasterScript singleton {get; private set}
    3. public void Awake()
    4. {
    5. singleton = this
    6. }
    7. [//CODE]
    8.  
    9. Also this may not be the right subforum for this question.
     
  3. U-GeeN

    U-GeeN

    Joined:
    Mar 22, 2015
    Posts:
    92
    Is there a easy way to get player objects without searching for name or tag?
     
  4. Xuzon

    Xuzon

    Joined:
    Mar 21, 2014
    Posts:
    83
    I have a list of Players in the Synchronizer (I think that is your GameMasterScript) that on Awake of every Player subscribe itself in that list to not using tags and finding objects, that methods are too expensive

    As @Zullar says, I think this is not the best subforum to ask this XD