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

Instantiate creates two obects instead of one

Discussion in 'Scripting' started by ShowMe666, Apr 8, 2020.

  1. ShowMe666

    ShowMe666

    Joined:
    Mar 13, 2020
    Posts:
    7
    Hello, i am new at Unity, and i can't solve one issue for few days already, and have started over one project 10 times already................... sheeshh............
    So i created trigger object inside my road prefab that contains one more trigger and road itself. When car ontriggerenter with this object, i spawn new road prefab further z coordinate. I have one road prefab as starter, when my car hits the first road trigger event, it creates one object, when car hits trigger of the created prefab road, it creates two objects. So summary is, Instantiated objects triggers create two object instead of one, whhyyyyyyyyyyyyy???????? ohmygoshhhhhghggh so tired lol....
    I could make it through a backbone, but i want to know why it happens.

    Code (CSharp):
    1. public class CreateNextRoad : MonoBehaviour
    2. {
    3.     public GameObject Road;
    4.     static float nextSpawnPoint = 149f;
    5.  
    6.     private void OnTriggerEnter(Collider other)
    7.     {
    8.         if(other.CompareTag("Player"))
    9.         {
    10.             float parentPos = transform.parent.transform.position.z;
    11.  
    12.             Instantiate(Road, new Vector3(0f, 0f, nextSpawnPoint), Quaternion.Euler(0, 90, 0));
    13.  
    14.             nextSpawnPoint = parentPos + 109f;
    15.  
    16.             Debug.Log(nextSpawnPoint);
    17.         }
    18.     }
    19. }
     
  2. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    To make sure the instantiation process is only executed once, you have to make sure to use a Boolean variable to check before executing the onTriggerEnter code and set it to true inside that. If the road is instantiated, does it also instantiate something on Awake, Start or OnEnable method?

    If you're tired or frustrated, take a break. You might be overthinking it.
     
  3. ShowMe666

    ShowMe666

    Joined:
    Mar 13, 2020
    Posts:
    7
    Isn't boolean backbone in cases like this? I want to know why it happens. I enter trigger once, i even made trigger object flat instead of cube. I don't use Awake, Start etc so far.
     
  4. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    It does sound strange. Does the debug also only return once? Or twice?
     
  5. ShowMe666

    ShowMe666

    Joined:
    Mar 13, 2020
    Posts:
    7
    Debug log shows messages twice for static and 4 for Instantiated.

    This is hierarchy to make things more clear


    Ok, i don't know why, but now it one message for static and two for instantiated..
     
    Last edited: Apr 8, 2020
  6. ShowMe666

    ShowMe666

    Joined:
    Mar 13, 2020
    Posts:
    7
    Maybe my problem that i create objects in wrong order? I just need to understand what am i doing wrong, guys help...
     
  7. ShowMe666

    ShowMe666

    Joined:
    Mar 13, 2020
    Posts:
    7
    Ok.... 3 forums couldn't help me....


    I found out how to make everything proper.
    The idea is to call spawn next object method inside parent object from trigger child object.

    Parent Obj script:
    Code (CSharp):
    1. public class SpawnNextRoad : MonoBehaviour
    2. {
    3.     public GameObject Road;
    4.     static int roadCounter = 0;
    5.     public void SpawnFurtherRoad()
    6.     {
    7.         float nextSpawnPoint = transform.position.z + 109f;
    8.         Instantiate(Road, new Vector3(0f, -1f, nextSpawnPoint), Quaternion.Euler(0, 90, 0)).name = "Road" + roadCounter.ToString();
    9.         roadCounter++;
    10.     }
    11. }
    Trigger child script:
    Code (CSharp):
    1. public class triggerSpawnRoad : MonoBehaviour
    2. {
    3.     private void OnTriggerEnter(Collider other)
    4.     {
    5.         if(other.CompareTag("Player"))
    6.         {
    7.             transform.parent.GetComponent<SpawnNextRoad>().SpawnFurtherRoad();
    8.         }
    9.     }
    10. }
    11.  

     
    reza_b_mirzaei likes this.