Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Bug my Instantiate is broken

Discussion in 'Scripting' started by Xenfor, May 7, 2021.

  1. Xenfor

    Xenfor

    Joined:
    May 2, 2021
    Posts:
    4
    what should I do?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class NewBehaviourScript : MonoBehaviour
    5. {
    6.     Transform tr;
    7.     public GameObject ground;
    8.     public int groundlength;
    9.     void Start()
    10.     {
    11.         for (int i = 0; i < groundlength; i++)
    12.         {
    13.             Debug.Log("hello");
    14.             Instantiate(ground, transform.position, Quaternion.identity);
    15.         }
    16.        
    17.    
    18.     }
    19. }
    20.  
    image:



    I don't use the instantiate command, this is ok upload_2021-5-7_18-21-51.png
     
  2. Spudly1701

    Spudly1701

    Joined:
    Aug 25, 2013
    Posts:
    36
    You have your instantiation script attached to the prefab that you're trying to instantiate. So every time that object (zemin) is instantiated, it's added another clone of itself.

    Create a new Empty Game object in your hierarchy and attach the script to that. (and remove the script from zemin)
     
  3. Xenfor

    Xenfor

    Joined:
    May 2, 2021
    Posts:
    4
    thanks