- Home /
How can I fix this UnassignedReferenceException error?
using UnityEngine;
using System.Collections;
public class SlowWall : MonoBehaviour {
public GameObject Player;
void Example (){
Player = GameObject.Find("Player");
}
public float ShrunkenSlowDownPercentage = 0.6f;
public float SlowDownPercentage = 0.8f;
public float EnlargedSlowDownPercentage = 0.9f;
void OnTriggerEnter(Collider other)
{
print("Collision detected with trigger object " + other.name);
PlayerComponent playerComponent = other.gameObject.GetComponent<PlayerComponent> ();
//checking if collided with player
if (playerComponent) {
if (Player.transform.localScale.magnitude < 0.9f){
playerComponent.Speed *= ShrunkenSlowDownPercentage;
}
if (Player.transform.localScale.magnitude > 1.9f){
playerComponent.Speed *= EnlargedSlowDownPercentage;
}
else{
playerComponent.Speed *= SlowDownPercentage;
}
if (playerComponent.Speed < 30f){
playerComponent.Speed = 30f;
}
Destroy (gameObject);
}
}
void OnTriggerStay(Collider other)
{
print("Still colliding with trigger object " + other.name);
}
void OnTriggerExit(Collider other)
{
print(gameObject.name + " and trigger object " + other.name + " are no longer colliding");
}
}
Okay so here's the error UnassignedReferenceException: The variable Player of SlowWall has not been assigned. You probably need to assign the Player variable of the SlowWall script in the inspector. SlowWall.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Obstacles/SlowWall.cs:19)
Now here's the problem, the slowall script is for a generated object, so how can I get this generated object to atomatically assign my Player to it's script? Any help or advice is welcome and sorry, I'm new to programming.
Answer by CHPedersen · Nov 12, 2014 at 01:36 PM
The exception is telling you that you need to assign the Player variable to something, which is often done in the inspector by dragging the GameObject Player onto the variable "Player" in this script.
It's also possible to set it in code, and you actually have code in place that does just that: The call to GameObject.Find("Player") which takes place in method Example(). Unfortunately, Example() is never called anywhere, so the variable remains unset.
Set the variable in the inspector and the error goes away.
PS: I took the liberty to edit your question in order to format your code correctly. Unity Answers often does a terrible job highlighting code automatically on pasting, and will frequently miss leading or ending braces, etc. Next time, mark the code manually after pasting and hit the "101010" button.
The thing is that the script itself will only accept prefabs which doesn't work for this case. As I said it's a generated object so I need it to find and assign Player itself but I don't know how to
Ah, right. Then you just need to make sure the code that sets the Player actually gets called. If I were you, I think I would just rename "Example()" to "Start()". Then Unity will call it automatically when the script is loaded, and set the Player variable for you.
$$anonymous$$y pleasure. Thank you for marking the answer correct. :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unable to disable a C# script with a JS Script 2 Answers
Change AnimationCurve keyframe values from script 0 Answers
witch approach for an car race curve 1 Answer