Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Proper way to get references to playerspawns?

Discussion in 'Scripting' started by Napoleonite, Apr 5, 2015.

  1. Napoleonite

    Napoleonite

    Joined:
    Mar 15, 2015
    Posts:
    27
    I made a quick prefab so I can easily add playerspawns to the levels. However, how do I get the reference to these playerspawns? I don't want to use tags (unless you can convince me otherwise) because those loop through all of the gameobjects, slowing down the loading of a new level unnecessarily.

    Every playable level has one or more of these prefabs.

    I can't use the GameSettings singleton class because I can't drag the spawns from the level into the singleton (which is only created in the first scene, and then never destroyed). How does one do this properly?

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerSpawn : MonoBehaviour {
    6.  
    7.    #region Members
    8.   public string GizmoTexture = "PlayerSpawnGizmo.png";
    9.   public float Icon_Y_Offset = 1;
    10.   private const float PLAYER_HEIGHT = 3;
    11.   private const float FACING_ARROW_LENGTH = 1.5f;
    12.    #endregion
    13.  
    14.   void OnDrawGizmos()
    15.   {
    16.   Gizmos.DrawIcon(transform.position + (Icon_Y_Offset * Vector3.up), GizmoTexture);
    17.   Gizmos.color = Color.yellow;
    18.   Vector3 cube_center_addition = (PLAYER_HEIGHT / 2) * Vector3.up; // A vector3 that represents half the player-size upwards.
    19.   Gizmos.DrawWireCube(transform.position + cube_center_addition, new Vector3(1, PLAYER_HEIGHT, 1));
    20.   Gizmos.DrawLine(transform.position + cube_center_addition, transform.position + (FACING_ARROW_LENGTH * Vector3.forward) + cube_center_addition);
    21.   }
    22. }
    Note that the same problem applies to many other prefabs like EnemySpawners. I place, let's say 5 EnemySpawners in some towerdefense game in the level. And every 10 seconds I need to retrieve the reference to those spawners so that I know where to spawn them, what direction they face, etc.

    Or should I create a new singleton like public class LevelInfo that is recreated in every level? And that I drag that onto an empty gameobject in every level?
     
    Last edited: Apr 5, 2015
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,205
    What if they registered themselves to the appropriate system or list?

    Code (csharp):
    1.  
    2. void Awake()
    3. {
    4.      GameManager.AddSpawnPoint(this.gameObject);
    5. }
    6.  
     
    Napoleonite likes this.
  3. Napoleonite

    Napoleonite

    Joined:
    Mar 15, 2015
    Posts:
    27
    Why didn't I think of that... That is a fantastic idea!
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,508
    Or send a PlayerSpawned event when they spawn. Then any script can listen to that.
     
  5. Napoleonite

    Napoleonite

    Joined:
    Mar 15, 2015
    Posts:
    27
    Events could work. But in general I don't want to trigger every spawn for just one player or one enemy. In most of the cases I only need to address one single spawner (or other object) out of a list of many. I just want to say:

    spawnplayer(spawners.random, boltconnection)

    Now if each spawner just registers itself on Awake() then I have a guaranteed reference to that list without having to add something myself to every level.
     
unityunity