X
X
xlo2402021-04-07 23:31:45
Unity
xlo240, 2021-04-07 23:31:45

How to access a character with a script from another object?

There is a simple character animation control script. But it works when the script is attached to the character.
How to rewrite the code so that the script hangs on a neutral object? For example on the surface.

public class Manage_grl1 : MonoBehaviour
{
    Animator _animator_grl1;
      
    void Start()
    {
        _animator_grl1 = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.L))
        {
            KeyL();
        }
    }
    public void KeyL()
    {
        Debug.Log("Нажали L");
        _animator_grl1.SetBool("startwrestle_grl1", true);

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kokapuk, 2021-04-08
@xlo240

Create a public variable of type GameObject, for example, char, and in the inspector, set your character with animation to this variable, then you can get the animator component from the script:
_animator_grl1 = Char.GetComponent<Animator>();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question