Jim
Jim

Reputation: 801

Access vector3 from another script in unity c#?

Thanks in advance for any help or code you might have. I hope the below all makes sense.

My problem is that I would like to use a vector3 thumbStickInput stored in one script in an entirely different script without having to use getComponent on update (which is what I'm currently doing). It should be in start () I think? My current script is definitely wrong and a bit wasteful!

Here is the code, which works but isn't great/

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

    public Vector3 thumbStickInput;
    public PlayerInput gcPlayerInput;

    Vector3 targetRotation;
    public GameObject TankTurret, TankBase, Player;


    // Use this for initialization
    void Start () {


        var gcPlayerInput = Player.GetComponent<PlayerInput>(); 
        // --- Why won't this be registered to work on update? ---


    }

    // Update is called once per frame
    void Update () {

        var gcPlayerInput = Player.GetComponent<PlayerInput>(); // <-- BAD!
        thumbStickInput = gcPlayerInput.thumbStickInput; <-- BAD.



        if (thumbStickInput != Vector3.zero) {

            targetRotation = thumbStickInput;
            TankTurret.transform.rotation = Quaternion.LookRotation(targetRotation);

            print (thumbStickInput);
        }
}
}

To break it down further incase theres any confusion. I am storing player 1's thumb-stick axis in a Vector3 called thumbStickInput on a script called PlayerInput which is on a game object called Player.

In another script called movement on a game object lower in the hierarchy called playerTank I am trying to rotate an objecting using the beforementioned thumbStickInput.

Upvotes: 0

Views: 1219

Answers (1)

Ivan Fateev
Ivan Fateev

Reputation: 1061

Your script already contains member gcPlayerInput for caching component, which you are not using. Getting the component on Update is a bad practice as you said, so common way is to cache component on Start and then use it.

You should not use var keyword to assign a value to member of the object, because this means that you're declaring local variable with the same name. I guess unity should warn you about that

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

    public Vector3 thumbStickInput;
    public PlayerInput gcPlayerInput;

    Vector3 targetRotation;
    public GameObject TankTurret, TankBase, Player;


    // Use this for initialization
    void Start () {


        gcPlayerInput = Player.GetComponent<PlayerInput>(); // removed var
        // now it's cached and you can use it on update    

    }

    // Update is called once per frame
    void Update () {

        // var gcPlayerInput = Player.GetComponent<PlayerInput>(); // <-- BAD! ---- removed
        thumbStickInput = gcPlayerInput.thumbStickInput; <-- BAD.



        if (thumbStickInput != Vector3.zero) {

            targetRotation = thumbStickInput;
            TankTurret.transform.rotation = Quaternion.LookRotation(targetRotation);

            print (thumbStickInput);
        }
}
}

Upvotes: 1

Related Questions