Search Unity

Question Get Component from other gameobject

Discussion in 'Scripting' started by getmyisland_dev, Jul 9, 2021.

  1. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    In my OnTriggerEnter function I try to get a script from another gameObject. How can I do this.

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    Code (CSharp):
    1. PlayerManager tempTarget = other.GetComponent<PlayerManager>();
    Above you can see my current code, but this code only seeks in the current game object for the PlayerManager Component.

    How can I define a game Object like this:
    Code (CSharp):
    1. public GameObject myObject;
    and convert my script that it seeks in my defined game object for the PlayerManager Component.

    Thanks in advance
    -Max
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    I don't see the problem, it should just work the way you intended.
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    If it's a manager object, then you will only need one of it. That means you could create a singleton and save yourself from doing getcomponent, which is expensive.