Seperate script into Player and Hero scrpits

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 :slight_smile:

you need to create a new script file, not write all on the same

For the future, please post scripting questions in the scripting sub-forum here, not the 2D sub-forum. Also, please read the first post in that forum that explains how to use code-tags when posting code and not a wall of plain text.

Thanks.

I did in two different scripts one per class but my problem is that regidbody is not defined then

Sure I will do it better next time :slight_smile: