How can I access another script in my prefab to set a variable after my network player setup runs?

Hello all. I’ve run into a problem that seems like it should be super easy to solve, but that I’ve been stuck on for longer than I’d like to admit. I’m spawning a player on my network and running a player setup script that inherits from NetworkBehavior. In that script, I instantiate a camera prefab and set its target to be the local player that the network has just created. Then, I have a separate player control script that the player setup script disables for non-local players.

The issue is that, for local players, my player controller script needs a reference to the camera that is pointing at the player. It uses this direction for camera relative movement. It seems like there should be many ways to do this, but I have run into walls with all of them. I wanted to post to help me understand why I can’t do the things I’m trying to do and to find a solution.

First I tried to simply created a public var with the type ControllerScript and drag the controller script in the prefab to that reference, but the editor wouldn’t let me do this. Can you not use public vars to access user defined types in the same prefab? Then, I tried to access the ControllerScript via GetComponent() in the Start() method on my player setup script. This call always comes back null, though. Do all of the scripts not exist when start Start() is called? Finally, I thought I would just create the camera in player setup’s Start(), save its reference, and access that in PlayerController with some method that runs after Start(). I can’t find a method that always runs after Start() and before Update(), though. I also can’t move the camera creation in player setup to Awake() because isLocalPlayer isn’t set in Awake().

I feel that there is an easy solution, but I might be misunderstanding something and unable to see it. I’ve attached my player setup script here. All of these scripts are on a player prefab that the network is spawning. Any help is greatly appreciated.

public class PlayerSetup : NetworkBehaviour {

public Behaviour[] toDisable;
public GameObject camPrefab;

// Holds reference to camera after creation
public GameObject camRef;
public ThirdPersonUserControl tpc;

void Start () {

	// Turn off move scripts for all other players
	if (isLocalPlayer) {
		// Create camera and set it to follow this player
		camRef = Instantiate (camPrefab, transform.position, transform.rotation) as GameObject;
		camRef.GetComponent<CameraFollow> ().target = transform;

   // Whether I have this line or not, tpc ends up null
		GetComponent<ThirdPersonUserControl> ().m_Cam = camRef.transform;

		tpc = transform.GetComponent<ThirdPersonUserControl>();
		if (tpc != null) {
           // This line throws NullReferenceException without the if statement
			tpc.AssignCamera (camRef.transform);
		} else {
			Debug.Log ("controller ref never assigned");
		}
	} else {
		Debug.Log ("Not Local Player");
		for (int i = 0; i < toDisable.Length; ++i) {
			toDisable *.enabled = false;*
  •   	}*
    
  •   }* 
    
  • }*

So I figured out what was going on, and it had nothing to do with my question. I had two copies of a script with the same name in my project. Part of my prefab was referencing one from very early development. The behavior looked the same, but the classes that seemed like they should be the same weren’t actually compatible.

Lesson for me to be more careful managing my files. Heads up to everyone that Unity won’t complain about two classes with the same name if they are in different directories.