Search Unity

Destroy a prefab by Instantiating the same prefab

Discussion in 'Prefabs' started by HerreraFA15, Jan 7, 2020.

  1. HerreraFA15

    HerreraFA15

    Joined:
    Oct 29, 2019
    Posts:
    7
    Hello Community,

    I create a SkyMap with a bunch of stars and now I want to instantiate a button with the name of each star by clicking in that specific star. By the moment, everything looks great! I click a star, a button appears with the same name of the star but I want to know now is how I destroy the button of one star when clicking a different star?

    In the moment, if i just keep clicking different stars the buttons are going to clone in the same position. Is there a way to call Destroy in this case even though it is the same prefab?

    Thanks!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Why you need button, if you already clicking at the stars?

    Other option, is to move button to new star position, instead creating new.

    How do you create buttons?
     
  3. HerreraFA15

    HerreraFA15

    Joined:
    Oct 29, 2019
    Posts:
    7
    I just want to display the name of the star with a button function as an optional way to see the information of the star, or that is what Im thinking.

    I create the buttons like this
    Inside a Empty GameObject there is a sphere object with a script called StarSelectDown

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System;
    6.  
    7.  
    8. public class StarSelectDown : MonoBehaviour
    9. {
    10.  
    11.  
    12.     public GameObject nameSelected; //this is the button
    13.  
    14.     public void ClickDown()
    15.     {
    16.         var parentOBj = transform.parent.GetComponent<Transform>(); //this is just taking the name of the empty object that have the star name and see if it shows
    17.         Debug.Log(parentOBj.name + parentOBj.position + parentOBj.rotation);
    18.    
    19.        //here is the button
    20.         GameObject nameSelec = Instantiate(nameSelected) as GameObject;
    21.         nameSelec.transform.SetParent(transform, false);
    22.         nameSelec.name = parentOBj.name;
    23.         var selectbutton = nameSelec.GetComponentInChildren<Button>();
    24.         var selectedText = selectbutton.GetComponentInChildren<Text>();
    25.         selectedText.text = parentOBj.name;
    26.    
    27.     }
    28.  
    29. }
    btw, the prefab is displayed with all stars name using a cvs data. So, when running each sphere prefab will have a different star name.

    and I use this other code through the camera to click the displayed spheres to instantiate their own button name.
    I put this in the camera

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StarSelect : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private LayerMask selectionLayer;
    9.  
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.      
    15.         if (Input.GetMouseButtonDown(0))
    16.         {
    17.             Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);
    18.             RaycastHit rayHit;
    19.             if (Physics.Raycast(ray, out rayHit, Mathf.Infinity, selectionLayer))
    20.             {
    21.              
    22.              rayHit.collider.GetComponent<StarSelectDown>().ClickDown();
    23.              
    24.  
    25.            
    26.             }
    27.        
    28.         }
    29.    
    30.     }
    31. }
     
    Last edited: Jan 7, 2020
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Ok few things.

    To make life easier, please use Code Tags

    What you want in first part of code, is having UI element, which you can show and hide, rather instantiate and destroy. Then you only need to worry about setting position, name of selected star and other options.
     
  5. HerreraFA15

    HerreraFA15

    Joined:
    Oct 29, 2019
    Posts:
    7
    Can you give me an example of how to do that?
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Every game object has active status.
    you can use
    Code (CSharp):
    1. go.SetActive ( true / false ) ;
    Like in your case
    Code (CSharp):
    1. nameSelec.SetActive ( false ) ; // hide
    For position you use
    Code (CSharp):
    1. nameSelec.transform.position = myNewPosition ;
    or
    Code (CSharp):
    1. nameSelec.transform.localPosition = myNewPosition ;
    Same principle for setting parents etc.


    Please edit you post, to apply Code Tag.
     
    HerreraFA15 likes this.
  7. HerreraFA15

    HerreraFA15

    Joined:
    Oct 29, 2019
    Posts:
    7
    Thanks for the help!
    Btw, What is that Code Tag you talk about and How do i apply that to the post? XD
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    See blue italic link for details in my forth post:
     
    HerreraFA15 likes this.