Search Unity

Calling gameobjects

Discussion in 'Scripting' started by Hello258, Nov 27, 2017.

  1. Hello258

    Hello258

    Joined:
    Nov 3, 2017
    Posts:
    2
    I'm somewhat new to Unity and C# and I am having some trouble. Can someone please guide me?
    I have a 3D game object in my Unity scene ("Player") that has a script on it with a method allowing it to grow (transform). I also have a my VR controller object with a script taking input. What I'm trying to do is call the method on Player from the VR controller script. My problems are:
    1. I don't know how to reference the 'Player' game object / method.
    2. I'm still really confused about declaring / calling objects.
    3. I'm not sure my .transform code is correct.

    I appreciate your patience while I learn.

    My 2 scripts:

    PLAYERSCRIPT.CS (On Player)

    public class PlayerScript : MonoBehaviour
    { public Rigidbody Player;

    // This is my test method, trying to get the Player object to Flex (expand) when the left trigger is pulled on the vive controller.
    public void AtkFlex()
    {
    for (int count = 0; count < 20; count++)
    { Player.transform.localScale += new Vector3(0.1F, 0, 0.1F); }
    }

    void Start ()
    {
    Player = GetComponent<Rigidbody>();
    }


    ATTACKS.CS (On left Vive controller)

    [RequireComponent(typeof(SteamVR_TrackedObject))]
    public class Attacks: MonoBehaviour {

    SteamVR_TrackedObject trackedObj;
    SteamVR_Controller.Device device;

    void Awake () {
    trackedObj = GetComponent<SteamVR_TrackedObject>();
    }

    void FixedUpdate () {
    SteamVR_Controller.Device device = SteamVR_Controller.Input((int)trackedObj.index);
    if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
    {
    Debug.Log("You are activating 'TouchDown' on the left controller.");

    //These were my failed attempts to figure it out.
    //gameObject player = gameObject.GetComponent<PlayerScript>;
    //PlayerScript.AtkFlex();
    }
    }
    }
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    First off when you post code you should use the code brackets to make it easier to read. There's a dropdown in the reply box right between the insert video and save icons that assists with that.

    Your scaling of the transform looks mostly correct. Lets fix the way you attempted to do the part you had trouble with. Instead of a gameobject as your Type you need the Type to be the script you're referencing, like so:
    Code (CSharp):
    1. PlayerScript playerScript = GetComponent<PlayerScript>();
    2. playerScript.AtkFlex();
    Another option, if the script you're referencing is a singleton (there is only one of it in existence at a time), you can make a public static variable in the script that you can reference the script from.
    Code (CSharp):
    1. public class PlayerScript : MonoBehaviour{
    2. public GameObject player;
    3. Rigidbody rigidBody;
    4. public static PlayerScript instance;
    5.  
    6. public void AtkFlex(){
    7.     for (int count = 0; count < 20; count++){
    8.         player.transform.localScale += new Vector3(0.1F, 0, 0.1F);
    9.     }
    10. }
    11.  
    12. void Start (){
    13.     instance = this;
    14.     rigidBody = GetComponent<Rigidbody>();
    15. }
    Then when you want to access something in that script it's as simple as this:
    Code (CSharp):
    1. PlayerScript.instance.AtkFlex();
     
    Last edited: Nov 27, 2017
  3. Hello258

    Hello258

    Joined:
    Nov 3, 2017
    Posts:
    2
    @QuinnWinters , thank you so much for the assistance. It means a great deal to me! Thank you for the forum tip as well, I will insert my code properly now.
    As for your example code, after examining it for a bit, it's starting to make sense. I was using the wrong Type. I'm sure as I become more familiar with C#/Unity, I will be able to identify these things more quickly. But I was able to implement this code and run it successfully. Thank you again for your help!