Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to access a class from another game object

Discussion in 'Scripting' started by George S, Aug 20, 2009.

  1. George S

    George S

    Joined:
    Jul 28, 2009
    Posts:
    54
    Lacking some basic Unity C# knowledge, please help.

    How do I access a script class on another object?

    How do I include another class in C# with using?

    I have a chicken game object that has this C# script on it.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ChickenBehaviour : MonoBehaviour {
    6.     public float speed = 8f;
    7. }
    8.  
    In another object, how can I get the ChickenBehaviour class and see it's members?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. // ??????? how should the using be done ???????
    4. using ChickenBehaviour;  // this fails
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.   void Update ()    {
    9.     if (Input.GetButtonDown("Fire1")) {
    10.       Ray theRay = playerCamera.ScreenPointToRay(Input.mousePosition);
    11.       RaycastHit hit;
    12.       bool mouseHitSomething = false;
    13.       if (Physics.Raycast (theRay, out hit)) {
    14.         // ??????? how should the class be accessed ???????
    15.         // don't know if this fails, can't get past using
    16.         ChickenBehaviour behave = hit.transform.gameObject.GetComponent(ChickenBehaviour);
    17.         Debug.Log("clicked on chicken with speed " + behave.speed);
    18.     }
    19.   }
    20. }
    21.  
     
  2. pavlito

    pavlito

    Joined:
    Nov 27, 2007
    Posts:
    136
    Maybe this helps...

    Code (csharp):
    1.  
    2. public GameObject playerObj;
    3.  
    4. void Start () {
    5.         // Find the Object with tag "player" and get data from it
    6.         playerObj = GameObject.FindGameObjectWithTag("Player");
    7.         // Get
    8.         Player playerScript = (Player)playerObj.GetComponent(typeof(Player));
    9.         oldScore = playerScript.score;
    10.         username = playerScript.Username;
    11.     }
    With the chicken object selected, tag it ("Add tag...") in the inspector. It should be "untagged" by default.
     
  3. George S

    George S

    Joined:
    Jul 28, 2009
    Posts:
    54
    Thanks, that's it.

    The solution for my code example in C# is:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. // no using clause is required, the ChickenBehaviour class is available
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.   void Update ()   {
    8.     if (Input.GetButtonDown("Fire1")) {
    9.       Ray theRay = playerCamera.ScreenPointToRay(Input.mousePosition);
    10.       RaycastHit hit;
    11.       bool mouseHitSomething = false;
    12.       if (Physics.Raycast (theRay, out hit)) {
    13.         ChickenBehaviour behave = hit.transform.gameObject.GetComponent(typeof(ChickenBehaviour)) as ChickenBehaviour;
    14.  
    15.         Debug.Log("clicked on chicken with speed " + behave.speed);
    16.       }
    17.     }
    18.   }
    19. }
    20.  
    It would be nice if this information was available in documentation for C#.