Unable to reference player within an instantiated prefab

I’m trying to make an instantiated prefab get bigger when the player gets closer to it. I’m using this method:

public class getBigger : MonoBehaviour
{
public const float PLAYER_DISTANCE = 10f;
public player player;
public Vector3 PREFAB1;

void Start()
{
player = Object.FindObjectOfType();
}

public void Update () {
if (Vector3.Distance(player.transform.position, PREFAB1) < PLAYER_DISTANCE) {
GetBig();
}
}

public void GetBig() {
Debug.Log(“got bigger”);
}

}

The player class is made here in a player manager script on an empty game object in the scene like so:

public class playerManager : MonoBehaviour
{
public player player;
public static playerManager Instance { get; private set; } // static singleton

void Awake() {

if (Instance == null) { Instance = this; }
else { Destroy(gameObject); }
player = FindObjectOfType();

}

}

Why doesn’t it work? :frowning:

You should use code tags , and you should be more explicit about what thing “doesn’t work”. Do you get an error message? If so, when? Does it refer to a specific line of code? Does anything at all happen in your game? Can you confirm that specific parts of your code are or aren’t running by adding Debug.Log calls?

Right, sorry let me try and be more explicit. I don’t get any errors with this code but here is an error i get when trying to reference the player in a respawn script on a different instantiated prefab.

UnassignedReferenceException: The variable player of respawn has not been assigned.
You probably need to assign the player variable of the respawn script in the inspector.

I guess my player manager script isnt working. Is there a different way to reference the player within a prefab?

That error means you are trying to use a variable that hasn’t been given a value. There are lots of possible reasons that your attempts to give it a value might have failed, but AFAICT you don’t seem to have shown the code that is actually getting the error OR the code that is supposed to assign the variable, which makes it pretty much impossible to guess what you did wrong.

Ok I will more clearly define my problem and try again in a new thread thanks

Well, let me try and guess ))
I believe your should use more reliable method for referencing the player. This

player = Object.FindObjectOfType<player>();

actually enumerates all objects in your scene and checks every script on them if it is player script or not. And this will not work if player is initially disabled in the scene or spawned in another script start method.

You need to have something like

player = PlayerManager.currentPlayer;

where current player variable is set by manager or other script. With it, you may do things like this

yield return new WaitUntil(() => PlayerManager.currentPlayer != null);
Debug.Log("Hello, " + player.name);

in coroutines and many other nice things. I believe if you change your player referencing method the problem may go away forever

Thanks for the attempt! You’re definitely right. I’m just not sure what code my player manager script needs. Could you include that part? :slight_smile: