Where Do I Get The Reference Script From An Object To Another Script?

I am making a script where my character, known as MainPlayer, can move.
here is the script(attached file below). How do I find and get the refernce script from MainPlayer to put the script to use? Thanks,
Steel

3049635–228785–move script.cs (543 Bytes)

This is essentially the same question as in your other thread, but since you’re slightly more specific in this one, I’ll answer here.

Recall from the tutorials I’m certain you’ve watched that Unity uses scripts as components that get attached to GameObjects. These scripts all inherit from MonoBehavior so that they have access to framework functions like Update and Start and such. But when you name the script file, that is the class name you refer to objects (components) of that class by.

By simply attaching the file to your GameObject (in this case, we’ll assume that is the standard Character Controller provided by Unity in the Standard Assets), your script automatically has access to certain values like transform, gameObject, name, enabled, etc, as well as those methods we mentioned earlier. If you want to access anything else, you have to get a reference to it first.

You do that by using the GetComponent() method. GetComponent is a slightly expensive operation, so you want to avoid calling it every frame (i.e., inside Update) and instead do it when your object is initialized. So you might do:

private CharacterController controller;

void Start() {
    controller = GetComponent<CharacterController>();
}

void Update() {
    controller.doStuffHere();
}

Alternatively, you could always create a public CharacterController field, and assign the value manually in the Inspector. This doesn’t make sense when you know the components are both on the same object, but it is handy when you have 2 objects in the scene from the start and want to assign references between them, such as your main camera having a reference to your player object.

While you’re learning how to find references to script instances, it’s worth noting the execution order of event functions and the script execution order (don’t confuse them)

The execution order allows you to manually adjust what order the scripts run. You might need this if they’re initializing in the wrong order so they can’t reference ach other. Otherwise, the default behavior is that every Awake() runs in random order.

Generally the intended use seems to be to initialize/instantiate everything that’s self-referencing in a script in the Awake function. That’s anything that can’t come up empty because something else wasn’t ready. Then, the Start functions all run and there you can gather references to scripts the Awake functions initialized without needing to worry about whether or not what you’re referencing exists yet or is ready.

1 Like

thanks for explaining that! makes more sense, but what do i put for the character controller? I figured how to do the movement by directly making the file under MainPlayer, but now i am making a 1st and 3rd person camera; i need to reference them too. Is referencing easier when they are a child of the original file(they are for this case)

Probably by making a public reference and assigning the camera to that. If you only have one camera in your scene, you can access it with

Camera.main

Did you go through the scripting tutorials? This stuff is all covered fairly early on there.

There are a bunch of ways to get references. The easiest for a beginner is to drag and drop on the inspector.