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

How do I instantiate gameobject again after i destroy it?

Discussion in 'Scripting' started by beckse7en, Mar 22, 2020.

  1. beckse7en

    beckse7en

    Joined:
    Apr 14, 2019
    Posts:
    2
    Hi, i am new to C# and Unity.
    what i'm trying to do is after i click and destroy the game object, the new one will instantiate on a different way point. i have search for ways to do this but i'm stuck, and any advice or idea would be much appreciated.
    Code (CSharp):
    1. public class ClickAndDestroy : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.         DestroyBall();
    6.     }
    7.     void DestroyBall()
    8.     {
    9.         if (Input.GetMouseButtonDown(0))
    10.         {
    11.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    12.             if (hit.collider != null)
    13.             {
    14.                 if (hit.collider.gameObject == gameObject)
    15.                 {
    16.                     DestroyObject desScript = hit.collider.GetComponent<DestroyObject>();
    17.                     BallSpawn ballScript = hit.collider.GetComponent<BallSpawn>();
    18.                     if (desScript)
    19.                     {
    20.                         desScript.Remove();
    21.                         if(ballScript)
    22.                         {
    23.                             ballScript.SpawnItems();
    24.                         }
    25.                     }
    26.                 }
    27.                 else if (hit.collider.gameObject != gameObject)
    28.                 {
    29.                     print("you lose");
    30.                 }
    31.             }
    32.         }
    33.     }
    34. }
    Code (CSharp):
    1. public class BallSpawn : MonoBehaviour
    2. {
    3.     public Transform[] spawnPoints;
    4.     public GameObject[] balls;
    5.  
    6.     void Start()
    7.     {
    8.         SpawnItems();
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.        
    14.     }
    15.  
    16.     public void SpawnItems()
    17.     {
    18.         int spawnIndex = Random.Range(0, spawnPoints.Length);
    19.         int spawnObject = Random.Range(0, balls.Length);
    20.  
    21.         Instantiate(balls[spawnObject], spawnPoints[spawnIndex].position, spawnPoints[spawnIndex].rotation);
    22.     }
    23. }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    In your SpawnItems() method, do you use ball prefab, to spawn from?
    I suspect, you use existing balls for instantiation, see second script line 21.

    Here is one of few way you can do it.
    https://docs.unity3d.com/Manual/InstantiatingPrefabs.html
    In inspector of InstantiationExample.cs, you just need attach your prefab, in myPrefab field.
     
    Dextozz likes this.
  3. beckse7en

    beckse7en

    Joined:
    Apr 14, 2019
    Posts:
    2
    hi i use the ball prefab to spawn from. the problem i got is that when i click on the ball and destroy it the next one doesn't pop up in the next way point.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    I suspect issue with random spawn object. Try set it to 0 and see if any results.