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

Instantiating independent from the prefab gameobjects

Discussion in 'Scripting' started by maximalniq, Jan 5, 2018.

  1. maximalniq

    maximalniq

    Joined:
    Feb 25, 2014
    Posts:
    43
    I am instantiating a Cube, when I press the right button, I want to activate a UI panel which is used for the cube only. But when I right click on the cube it activates all the UI panels for all the cubes that I instantiated on the scene, including the prefab one. How can activate the UI for the exact cube that I am clicking on ?

    The code for instantiating the cube.
    Code (CSharp):
    1. Instantiate(prefabPlacementObject, point, Quaternion.identity);
    The code for the right click.
    Code (CSharp):
    1. if (Input.GetMouseButtonDown(1))
    2.         {
    3.             gameObject.transform.GetChild(0).gameObject.SetActive(true);
    4.         }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Take you if statement out of update. That's why it is running on all the cubes. Just do this instead;
    Code (CSharp):
    1.     public void OnMouseDown()
    2.     {
    3.         gameObject.transform.GetChild(0).gameObject.SetActive(true);
    4.     }
     
    maximalniq likes this.
  3. maximalniq

    maximalniq

    Joined:
    Feb 25, 2014
    Posts:
    43
    Thank you. Is there a function for right mouse button not left ?
     
  4. maximalniq

    maximalniq

    Joined:
    Feb 25, 2014
    Posts:
    43
    It worked with OnMouseOver()


    Code (CSharp):
    1. private void OnMouseOver()
    2.     {
    3.         if (Input.GetMouseButtonDown(1))
    4.         {
    5.             gameObject.transform.GetChild(0).gameObject.SetActive(true);
    6.         }
    7.     }
    Thank you !
     
    whileBreak likes this.