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

Activating a gameObject using the same script but attached to multiple gameObjects

Discussion in 'Scripting' started by farooqiasfandyar, Apr 23, 2021.

  1. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
    Hi there,
    I'm building a shooter game and the weapon equip system is similar to Call of Duty Zombies. Like when you hover over a weapon case a UI text element appears and when out of range the text element disappears.

    I've been activating/deactivating the UI Text by accessing it as a gameObject.
    Problem:
    With one weapon case it works fine. But when i use multiple weapon cases all having the same script, the text element isn't activating when i hover over a case. I know it's because while one gameObject tries to activate it the remaining gameObjects deactivate it.

    Help is appreciated.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,138
    Show some code....and use code tags. :D
     
  3. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    you could give your text a script that gets all weapon cases assigned in a list

    like
    List<WeaponCase> cases;
    assuming that your weapon case class is called WeaponCase

    in awake or start you then gather all weapon cases currently active in your scene like:

    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4.    GameObject[] allWeaponCases =GameObject.FindGameObjectsWithTag("WeaponCaseTag");
    5.    foreach(GameObject obj in allWeaponCases)
    6.    {
    7.       cases.Add(obj.GetComponent<WeaponCase>());
    8.    }
    9. }
    10.  
    this code assumes that you have given your weapon case prefab the tag "WeaponCaseTag", thus making it possible to easily find all weaponCases in code (for this just click on your weapon case in unity and look at the Tag field in the top of the inspector)

    a function like this also need to be called again if you remove or add new weapon cases during runtime

    now that your script knows all the weapon cases, you can steer the visibility of your text with:

    Code (CSharp):
    1.  
    2. bool OneIsCloseEnough()
    3. {
    4.    foreach(WeaponCase weaponCase in cases)
    5.    {
    6.       float distance= (player.position - weaponCase.transform.position).magnitude;
    7.  
    8.       if(distance<visibilityDistance)
    9.       {
    10.          return true;
    11.       }
    12.    }
    13.    return false;
    14. }
    15.  
    16. void ChangeVisibility()
    17. {
    18.    if(OneIsCloseEnough())
    19.       textElementObject.SetActive(true);
    20.    else
    21.       textElementObject.SetActive(false);
    22. }
    23.  
    this means your text will only be visible if at least 1 WeaponCase is close enough

    since you can also get the specific weapon case that is close enough that way (the line where we return true), you could than additionally read out whatever info you need from that specific weapon case aswell
     
    Last edited: Apr 23, 2021
  4. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
  5. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
  6. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
    Thanks a lot!
     
  7. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
    Hey, I've just tried this but it says no reference set. So i did this:
    Code (CSharp):
    1. List<WeaponEquipScript> weaponCase = new List<WeaponEquipScript>();
    2.  
    3. Start()
    4. {
    5. GameObejct[] allWeaponCases=GameObejct.FindObjectsWithTag("WeaponCase'");
    6. foreach(GameObject obj in allWeaponCases)
    7. {
    8. weaponCase.Add(obj.GetComponent<WeaponEquipScript>());
    9. }
    10.  
    11. }
    Here my equipScript is "WeaponEquipScript". But this also doesn't seem to work
     
  8. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    is the tag you defined for your weapons cases really called WeaponCase' ?
    or is the ' in the name a typo?

    in any way, I assume that the no reference issue is simply that you have never initialized the weapon case list
    befor adding anything to it, it must be a list object first
    like
    cases = new List<WeaponEquipScript>();

    (this is not neccessary if the variable is serialized)
     
  9. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
    Umm I'm new to Lists, can you guide me how to do that, or suggest any other way of doing the job other than Lists.

    **
    I have the text activating script attached to a weaponContainer which is a Parent to all my Weapon Cases with the WeaponEquipScript attached to each case.
    **
    Thanks btw
     
  10. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    its just that one line that would be added to start or awake:

    Code (CSharp):
    1. void Start()
    2. {
    3.    cases = new List<WeaponCase>();
    4.    GameObject[] allWeaponCases =GameObject.FindGameObjectsWithTag("WeaponCaseTag");
    5.    foreach(GameObject obj in allWeaponCases)
    6.    {
    7.       cases.Add(obj.GetComponent<WeaponCase>());
    8.    }
    9. }
    if a list is never initialized with "new List<classType>();" it is simply null, instead of being an empty list

    if your list is public or [SerializeField] it is automaticly not-null (so it can be displayed in the inspector)

    so either add that one line to start or make the list public or SerializeField
     
    farooqiasfandyar likes this.
  11. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
    I fixed it using a Trigger Collider:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.    if(other.tag=="player")
    4.   {textElementObject.SetActive(true); }
    5. }
    6.  
    7. void OnTriggerExit(Collider other2)
    8. {
    9.    if(other2.tag=="player")
    10.   {textElementObject.SetActive(false); }
    11. }
    Thanks for your time and concern anyways.