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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problems with instantiating new object prefabs

Discussion in 'Editor & General Support' started by ZTheCdr, Apr 25, 2020.

  1. ZTheCdr

    ZTheCdr

    Joined:
    Apr 25, 2020
    Posts:
    6
    Code (CSharp):
    1. public class BallSpawner : MonoBehaviour
    2. {
    3.     public GameObject ball;
    4.  
    5.     private System.Random random = new System.Random();
    6.     private Vector3 spawnPos;
    7.  
    8.     // Update is called once per frame
    9.     void FixedUpdate()
    10.     {
    11.         if (Input.GetKey("r")) {
    12.             spawnPos = new Vector3(random.Next(-7, 7), 35, 21);
    13.        
    14.             Instantiate(ball, spawnPos, Quaternion.identity);
    15.         }
    16.     }
    17. }
    I am trying to spawn in a new ball at a random x coordinate (-7 to 7), and then at 35 y and 21 z. However, the ball always spawns at the same x coordinate even though I am randomly generating a new one for each ball spawn. The x coordinate the newly instantiated ball always spawns at is the starting x position of the original ball (basically, there is one starting ball, and then you can press "r" to spawn in more, and I want those to spawn at random x locations -7 to 7).

    Why is it doing this, and what should I do to get it to do what I want?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    There's not really any reason this shouldn't work... This should really be in Update() rather than FixedUpdate() though.

    Are there any scripts attached to the ball object? Is the ball a prefab, or an object in the scene hierarchy?
     
  3. ZTheCdr

    ZTheCdr

    Joined:
    Apr 25, 2020
    Posts:
    6
    The ball is a prefab. The ball has a script on it called moveBall. Here it is: any help is appreciated.
    Code (CSharp):
    1. public class MoveBall : MonoBehaviour {
    2.  
    3.  
    4.  
    5.     public Rigidbody rb;
    6.  
    7.     private Vector3 StartPos = new Vector3(-4, 35, 21);
    8.     private Vector3 Move = new Vector3(10, 0, 0);
    9.     private bool Dropped = false;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start() {
    13.         transform.position = StartPos;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void FixedUpdate() {
    18.  
    19.         if (!Dropped) {
    20.             if (Input.GetKey("a") && transform.position.x > -7) {
    21.                 transform.position -= Move * Time.fixedDeltaTime;
    22.             }
    23.  
    24.             if (Input.GetKey("d") && transform.position.x < 7) {
    25.                 transform.position += Move * Time.fixedDeltaTime;
    26.             }
    27.         }
    28.         if (Input.GetKey("space")) {
    29.             Dropped = true;
    30.             rb.useGravity = true;
    31.         }
    32.  
    33.     }
    34. }