Hi everyone,
I have an experience with programming, however I am completely new with Unity and I have probably very basic question, but I can’t find the answer anywhere.
So my problem is that I created a script PlayerController connected with the main character that suppose to move. It got quite big and I wanted to refactor it, because it was a mess already. I decided to create two different classes (scripts): PlayerController and a hero - Wizard in my case.
In short, it looks like that:
public class PlayerController : MonoBehaviour
{
private Wizard _wizard;
private void Start()
{
_wizard = gameObject.AddComponent();
}
private void Update()
{
Move();
}
private void Move()
{
if (Input.GetAxis(“Horizontal”) < 0)
{
_wizard.MoveLeft();
}
}
}
class Wizard : MonoBehaviour
{
private new Rigidbody2D rigidbody;
public Wizard()
{
rigidbody = GetComponent();
}
public void MoveLeft()
{
rigidbody.velocity = new Vector2(1, rigidbody.velocity.y);
transform.localScale = new Vector2(1, 1);
}
}
And PlayerController is a component of the hero in unity and wizard is not, it is a reason why game thinks regidbody is not defined, I don’t know how to change it, could you give me an advice how to do this properly?
Thanks in advance ![]()