color is not changing in enum code
I can't get this script to work, I am trying to get an object to change color based on what I drag into it. I don't get errors when I build the code but I do when I play the game and try to drag in the objects. Please help
(edit) Here are the errors I get
NullReferenceException: Object reference not set to an instance of an object CollisionAction.DoRed (UnityEngine.GameObject destroyObject) (at Assets/Scripts/CollisionAction.cs:48) CollisionAction.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/CollisionAction.cs:29)
NullReferenceException: Object reference not set to an instance of an object CollisionAction.DoBlue (UnityEngine.GameObject destroyObject) (at Assets/Scripts/CollisionAction.cs:65) CollisionAction.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/CollisionAction.cs:26)
bool activated;
int value;
private MeshRenderer wallRenderer;
private Material wallMaterial;
private CollisionProperty colPop;
void Awake (){
wallRenderer = GetComponent<MeshRenderer> ();
wallMaterial = wallRenderer.material;
colPop = GetComponent<CollisionProperty> ();
}
void OnCollisionEnter(Collision other){
CollisionProperty cp = other.gameObject.GetComponent<CollisionProperty> ();
if (cp != null){
switch (cp.myProp)
{
case CollisionProperty.PropType.blue:
DoBlue (other.gameObject);
break;
case CollisionProperty.PropType.red:
DoRed (other.gameObject);
break;
case CollisionProperty.PropType.green:
DoGreen ();
break;
case CollisionProperty.PropType.yellow:
DoYellow();
break;
case CollisionProperty.PropType.goal1:
DoG1();
break;
case CollisionProperty.PropType.goal2:
DoG2();
break;
}
}
}
public void DoRed(GameObject destroyObject){
if (activated == false && (colPop.myProp != CollisionProperty.PropType.red)) {
activated = true;
Destroy (destroyObject.gameObject);
wallMaterial.color = Color.red;
colPop.myProp = CollisionProperty.PropType.red;
StartCoroutine (countdown ());
} else if (colPop.myProp == CollisionProperty.PropType.red) {
//add more points because it is the same color
} else {
activated= true;
Destroy (destroyObject.gameObject);
wallMaterial.color = Color.red;
//just changed color
}
}
public void DoBlue(GameObject destroyObject){
if (activated == false && (colPop.myProp != CollisionProperty.PropType.blue)) {
activated = true;
Destroy (destroyObject.gameObject);
wallMaterial.color = Color.blue;
colPop.myProp = CollisionProperty.PropType.blue;
StartCoroutine (countdown ());
} else if (colPop.myProp == CollisionProperty.PropType.blue) {
//add more points because it is the same color
} else {
activated= true;
Destroy (destroyObject.gameObject);
wallMaterial.color = Color.blue;
//just changed color
}
}
public void DoGreen(){
}
public void DoYellow(){
}
public void DoG1(){
}
public void DoG2(){
}
IEnumerator countdown()
{
yield return new WaitForSeconds(5);
activated = false;
Debug.Log ("false");
}
}
what is the error? you can also add Debug.Logs and check the log file to help deter$$anonymous$$e the problem. It might be trying to access an object that has been recently destroyed.
NullReferenceException: Object reference not set to an instance of an object CollisionAction.DoBlue (UnityEngine.GameObject destroyObject) (at Assets/Scripts/CollisionAction.cs:65) CollisionAction.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/CollisionAction.cs:26)
NullReferenceException: Object reference not set to an instance of an object CollisionAction.DoRed (UnityEngine.GameObject destroyObject) (at Assets/Scripts/CollisionAction.cs:48) CollisionAction.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/CollisionAction.cs:29)
Ye it's trying to destroy a null object. I don't know why though. Are there many overlapping colliders? I mean, if you drop the object onto 3 colliders, it will only work once cause it gets destroyed.
An immediate bandage to remove the error would be to check if the object exists before destroying it. Though, this doesn't really solve the problem, more like ignoring it. You need to figure out why destroyObject is null.
if (destroyObject != null) Destroy(destroyObject);
Your answer
Follow this Question
Related Questions
How can I change the alpha value of multicolored text to 0? 1 Answer
Get color between 2 colors 0 Answers
change color 2 Answers
How to Change the Color of Text without using Markup Text? 0 Answers
Need help to make a color replace shader 0 Answers