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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with my Instantiate

Discussion in 'Scripting' started by SaamBell, Aug 9, 2015.

  1. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    Okay, so the problem here is not that the Instantiate function not working, but with me needed to have more than one carPos (the only one at the minute being -3.4, 3.4). Using these values works fine for all of my vehicles except one that is two cars side by side, as sometimes this car will overlap onto the side of my road.

    Here is a photo of what Is happening. Is there anyway I can get this specific vehicle in my array to use the Pos -3.0, 3.0 or something similar to keep it within bounds?


    help.png



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class carSpawner : MonoBehaviour {
    5.  
    6.     public GameObject[] cars;
    7.  
    8.     public uiManager ui;
    9.     int carNo;
    10.  
    11.     public float maxPos = 3.0f;
    12.     public float delayTimer;
    13.     float timer;
    14.  
    15.  
    16.     void Start ()
    17.  
    18.     {
    19.  
    20.         timer = delayTimer;
    21.     }
    22.  
    23.     void Update ()
    24.  
    25.     {
    26.         timer -= Time.deltaTime;
    27.         if(timer <= 0)
    28.         {
    29.         Vector3 carPos = new Vector3(Random.Range(-3.4f, 3.4f), transform.position.y,transform.position.z);
    30.      
    31.      
    32.         carNo = Random.Range (0,7);
    33.  
    34.         Instantiate (cars[carNo], carPos, transform.rotation);
    35.  
    36.  
    37.  
    38.         timer = delayTimer;
    39.  
    40.  
    41.  
    42.  
    43.         }
     
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Well first generate carNo and if it is the 2car then set carPos range 3.0 to -3.0 else keep 3.4

    EDIT to generate randomrange of carpos use if carno is two cars set range of 3.0 else 3.4
     
  3. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    Code (CSharp):
    1. void Update ()
    2.    
    3.     {
    4.         timer -= Time.deltaTime;
    5.         if(timer <= 0)
    6.         {
    7.         Vector3 carPos = new Vector3(Random.Range(-maxPos, maxPos), transform.position.y,transform.position.z);
    8.         Vector3 DuocarPos = new Vector3(Random.Range(-3.0f, 3.0f), transform.position.y,transform.position.z);
    9.        
    10.        
    11.         carNo = Random.Range (0,7);
    12.  
    13.         Instantiate (cars[carNo], carPos, transform.rotation);
    14.  
    15.             if(carNo == 7)
    16.             {
    17.                 carPos = DuocarPos;
    18.             }
    19.  
    20.         timer = delayTimer;
    tried this and had no luck
     
  4. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    You're changing carPos after instantiate?
     
  5. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    I tried the same code before the instantiate and still had no luck
     
  6. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    Corect me I'd I'm wrong. But doesn't Random.Range(0,7); return etter 0,1,2,3,4,5 or 6, not 7?
     
  7. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    So like this?

    Code (CSharp):
    1. void Update ()
    2.  
    3.     {
    4.         timer -= Time.deltaTime;
    5.         if(timer <= 0)
    6.         {
    7.         Vector3 carPos = new Vector3(Random.Range(-maxPos, maxPos), transform.position.y,transform.position.z);
    8.         Vector3 duoCarPos = new Vector3(Random.Range(-3.0f, 3.0f), transform.position.y,transform.position.z);
    9.      
    10.      
    11.      
    12.         carNo = Random.Range (0,7);
    13.  
    14.             if(carNo == 6)
    15.             {
    16.                 carPos = duoCarPos;
    17.             }
    18.  
    19.         Instantiate (cars[carNo], carPos, transform.rotation);
    20.  
    21.  
    22.         timer = delayTimer;
     
    phoda likes this.