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

Only Set This Child of This Prefab Active?

Discussion in '2D' started by Steelshot, Oct 23, 2021.

  1. Steelshot

    Steelshot

    Joined:
    Feb 24, 2015
    Posts:
    102
    Hi,

    So I have a prefab set up like this:

    employeePrefab
    - Design (Sprite)
    - employeeCanvas (Canvas)
    ---EmployeeMenu (BackgroundImage, container)
    -----T_Roles (Text with "Roles")
    -----B_Barista (Button to change employee role to Barisa)
    -----B_Front(Same as above but Front)
    -----B_Support(Same as above but Support)

    The employeePrefab has a script called EmployeeAI that manages the roles, movement, and attributes of an Employee. I'm having some trouble managing the EmployeeMenu. Basically, when I right-click an EmployeePrefab, I set the EmployeeMenu to be active.

    However, when I have multiple employeePrefabs in the scene, I right-click on one employeePrefab, then all EmployeeMenu objects in the scene are set to active. I only want the EmployeeMenu of the employeePrefab I right-clicked to be set active, how could I do this?

    script: https://hastebin.com/qaqayelago.csharp
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
  3. Steelshot

    Steelshot

    Joined:
    Feb 24, 2015
    Posts:
    102
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    3a5.jpg

    But on a more serious note, you should study line 142 and reason about the values you are passing to Physics2D.Raycast(), particularly the second one called "direction." :)

    Edit: instead of casting in the plane for touches, I think you want something like this:

    https://answers.unity.com/questions/577314/how-to-detect-if-a-sprite-was-object-was-touched-i.html

    Code (csharp):
    1. // each employee would check his own collider2d
    2. if (Input.touchCount > 0)
    3.  {
    4.     Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    5.     if (collider2D.OverlapPoint(wp))
    6.     {
    7.         //your code
    8.         Debug.Log ("Hello");
    9.     }
    10.  }
    But obviously for mouse, not touch... I'll let you hack that.
     
    Last edited: Oct 24, 2021