Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Spawning object at gameobjecs' position.

Discussion in 'Scripting' started by denissuu, Dec 15, 2019.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there everyone!So, I'm working at a game in which I want to spawn 10 pages at different positions that I have predefined via gameobjects.There are currently 16 of these positions where they could spawn, but they just seem to spawn at the same position inside one gameobject and it's not even the correct position.

    Here is what I tried:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PageGeneration : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.  
    9.     public Transform[] spawnSpots;
    10.     public GameObject Page;
    11.     private int randomSpawnSpot;
    12.     private Transform PagePos;
    13.     private int x = 0;
    14.     void Start()
    15.     {
    16.         randomSpawnSpot = Random.Range(0, spawnSpots.Length);
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         PageGen();
    23.     }
    24.  
    25.     void PageGen()
    26.     {
    27.         randomSpawnSpot = Random.Range(0, spawnSpots.Length);
    28.         while(x<=9)
    29.         {
    30.             Instantiate(Page, spawnSpots[randomSpawnSpot].transform);
    31.             x++;
    32.         }
    33.     }
    34. }
    35.  
    How could I make it work as intended?
    I don't really have any inspiration about how to do this, I have been trying for a few hours, but no luck.
    I am still kind of new to Unity overall and C#, so if you make any suggestions, please explain them a bit further so I can understand better.
    Thank you! :)
     
  2. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Try

    Code (CSharp):
    1. Instantiate(Page, spawnSpots[randomSpawnSpot].position, Quaternion.identity);
    Going off the docs:

    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    You are only passing in two values, the object and the parent it will be assigned to. So it will be created and its position will be assigned based on the parent transform you are passing in. Have a look at the top of the page i linked you. You need to pass in three arguments for the second one to be the position.
     
    denissuu likes this.
  3. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    Thank you for the tip!
    The thing is that now, they are still spawning all at the same spot, even though the spot now is one of the actual spawnSpot gameObjects.I don't really get why that is happening.
     
  4. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Are you sure all the spawnSpots are in the array? Check the array in the inspector. You might want to have an array of Vector3s instead of an array of transforms, that way you can store the position instead of the transform and you'll be able to view the positions in the inspector.
     
  5. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Oh right i see. You need to put the randomSpawnSpot calculation line inside the while loop so that it is called every iteration. At the moment it is called only once and never changes.

    Put this line:

    Code (CSharp):
    1. randomSpawnSpot = Random.Range(0, spawnSpots.Length);
    before this line:

    Code (CSharp):
    1. Instantiate(Page, spawnSpots[randomSpawnSpot].transform);
    Full function:

    Code (CSharp):
    1. void PageGen()
    2.     {
    3.      
    4.         while(x<=9)
    5.         {
    6.             randomSpawnSpot = Random.Range(0, spawnSpots.Length);
    7.             Instantiate(Page, spawnSpots[randomSpawnSpot].transform);
    8.             x++;
    9.         }
    10.     }
    Also you can put your PageGen in your start method instead of your update method.
     
    denissuu likes this.
  6. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    Hmm, it seems to work fine now.But how could I make it so there can't be 2 pages in the same place?

    Edit: It seems that it doesn't apply my set rotation of the gameobjects to the pages when they get spawned.What could I do about the Quaternion to make it keep the rotation of the gameobject?
     
    Last edited: Dec 15, 2019