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. Dismiss Notice

[Solved] Problem with adding GameObject positions to Vector3 array

Discussion in 'Scripting' started by colruytXD, Apr 29, 2016.

  1. colruytXD

    colruytXD

    Joined:
    Sep 20, 2015
    Posts:
    10
    Hi,

    It might be a simple question but how do I add GameObjects their position to a Vector3 array.

    I tried doing:
    Code (CSharp):
    1.             for (int i = 0; i < SpawnCubes.Length; i++)
    2.             {
    3.                 //Sets spawnpositions to all the right values
    4.                 SpawnPositions[i] = SpawnCubes[i].transform.position;
    5.             }
    But it didn't work :/

    Hope you can help!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    what happens?

    Do you get an error?
     
  3. colruytXD

    colruytXD

    Joined:
    Sep 20, 2015
    Posts:
    10
    NullReferenceException: Object not reference not set to instance of an object.

    Here you have the full script:
    Code (CSharp):
    1. public class GameEvent_SpawnShootable : MonoBehaviour {
    2.  
    3.         private GameEvents_Master gameEventsMaster;
    4.  
    5.         public float minTimeBetweenSpawn = 0.5f;
    6.         public float maxTimeBetweenSpawn = 2f;
    7.  
    8.         private float LastSpawnTime;
    9.         private float SpawnRate;
    10.         private float nextSpawn;
    11.  
    12.         private Vector3[] SpawnPositions;
    13.  
    14.         public GameObject[] SpawnCubes;
    15.  
    16.         public GameObject ShootableObject;
    17.  
    18.         void Start ()
    19.         {
    20.    
    21.         }
    22.    
    23.         void Update ()
    24.         {
    25.             if(Input.GetKeyDown(KeyCode.A))
    26.             {
    27.                 gameEventsMaster.CallEventSpawnCube();
    28.             }
    29.         }
    30.  
    31.         void OnEnable()
    32.         {
    33.             SetInitialReferences();
    34.             gameEventsMaster.EventSpawnCube += SpawnCube;
    35.         }
    36.  
    37.         void OnDisable()
    38.         {
    39.             gameEventsMaster.EventSpawnCube += SpawnCube;
    40.         }
    41.  
    42.         void SetInitialReferences()
    43.         {
    44.             gameEventsMaster = GetComponent<GameEvents_Master>();
    45.             SpawnCubes = GameObject.FindGameObjectsWithTag("CubeSpawn");
    46.  
    47.             for (int i = 0; i < SpawnCubes.Length; i++)
    48.             {
    49.                 //Sets spawnpositions to all the right values
    50.                 SpawnPositions[i] = SpawnCubes[i].transform.position;
    51.             }
    52.         }
    53.  
    54.         void SpawnCube()
    55.         {
    56.             int RandomCount = Random.Range(0, SpawnPositions.Length);
    57.             Instantiate(ShootableObject, SpawnPositions[RandomCount], Quaternion.identity);  
    58.         }
    59.     }
     
  4. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Ok so based on my quick glimpse, maybe there isn't a tag in the gameobjects that you're searching for. Thereby, leaving the gameobjects array empty.
     
  5. colruytXD

    colruytXD

    Joined:
    Sep 20, 2015
    Posts:
    10
    it says it's on line 55
     
  6. colruytXD

    colruytXD

    Joined:
    Sep 20, 2015
    Posts:
    10
    I don't have much else, i just started. I can show you the Master script if you want:
    Code (CSharp):
    1.     public class GameEvents_Master : MonoBehaviour {
    2.  
    3.         public delegate void GameEventHandler();
    4.  
    5.         public event GameEventHandler EventSpawnCube;
    6.  
    7.         public void CallEventSpawnCube()
    8.         {
    9.             if(EventSpawnCube != null)
    10.             {
    11.                 EventSpawnCube();
    12.             }
    13.         }
    14.     }
     
  7. colruytXD

    colruytXD

    Joined:
    Sep 20, 2015
    Posts:
    10
    I have the tag "SpawnObject" attached to 9 gameObjects. It puts them in the array. That i can see in the inspector. It's really weird
     
  8. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Ah sorry. Even the error I can't decipher. Wait till someone who knows more than me come around.
     
  9. colruytXD

    colruytXD

    Joined:
    Sep 20, 2015
    Posts:
    10
    No problem. Thanks for trying! :)
     
  10. galelo

    galelo

    Joined:
    Feb 13, 2016
    Posts:
    7
    try to manually put the gameobject and see how that works
    remove the SpawnCubes =GameObject.FindGameObjectsWithTag("CubeSpawn");
    and manually drag and drop the gameobject to the script
     
  11. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    There is no way the line of code

    Code (CSharp):
    1.  
    2.         private Vector3[] SpawnPositions;
    3.  
    will find out how many elemets without you declaring it, so it's set to 0 initially.

    either as a public variable or like

    Code (CSharp):
    1.  
    2. [SerializeField]private Vector3[] SpawnPositions; // [SerializeField] makes private variables viewable in inspector, but can't be editied it's just to check if the values are getting stored.
    3. public GameObject[] SpawnCubes[]
    4.  
    5. public void Start()
    6. {
    7.   SpawnPositions = new vector3[SpawnCubes.length]; // declares size of SpawnPositions array to same size as SpawnCubes
    8. }

    This will set it to be the same size as the game objects, and the other code should work :)
     
    Last edited: Apr 29, 2016
  12. colruytXD

    colruytXD

    Joined:
    Sep 20, 2015
    Posts:
    10
    Thank you so much! Thanks to all you guys. U da real MVP
     
  13. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Could you change the title to solved, it's misleading
     
  14. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Welcome :)