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

Question Prefab Update() event responding simultaneously in all instances of my prefab.

Discussion in 'Prefabs' started by rosco_y, Jun 13, 2023.

  1. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    I have a 9x9x9 array of Prefabs (cubes with Text components.)



    Attached to my prefab is an Update() event as follows:

    Code (CSharp):
    1.  private void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(1))
    4.         {
    5.             if (_sudoValue < 0)
    6.                 this.SetCandidate(-_sudoValue, true);
    7.         }
    8.  
    9.         if (Input.GetMouseButtonDown(0))
    10.         {
    11.             if (_sudoValue < 0f)
    12.                 this.UserValue = -_sudoValue;
    13.         }
    14.     }
    When I wrote this code I was thinking the Update() event would apply separately--to each individual prefab, but when I click any single of my instantiated prefabs, it's exactly as if I'd clicked on every individual instantiated prefab at once. This seems to break the object-oriented paradigm. Can someone help me understand what I'm missing?

    Thank you,

    rosco_y
     
  2. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    284
    This code is just detecting whether the mouse buttons are down. There is no code to determine what is being clicked, just is the mouse button down, yes or no? Sorry for not pointing you in a better direction, but it's a pretty open ended topic.
     
    rosco_y likes this.
  3. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    CodeRonnie--Thanks for your quick reply! Your answer does make sense, and it is so much more rewarding than checking back daily to find no responses.

    While being careful to not sound gratuitous, Thank you again, I really appreciate your help!

    rosco_y
     
  4. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    284
    Happy to help. Also, I would move the input logic out of the prefabs, and into one central location where input is being handled, then do something like raycast from the mouse position on screen, converted to a ray in the world space of the scene. If that ray being cast into the scene collides with one of your prefabs, that is what the mouse is clicking on.