Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

color is not changing in enum script (solved)

Discussion in 'Scripting' started by grkpektis, Apr 20, 2016.

  1. grkpektis

    grkpektis

    Joined:
    Mar 24, 2016
    Posts:
    10
    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)
    Code (CSharp):
    1.  
    2.  
    3. bool activated;
    4. int value;
    5. private MeshRenderer wallRenderer;
    6. private Material wallMaterial;
    7. private CollisionProperty colPop;
    8. void Awake (){
    9.      wallRenderer = GetComponent<MeshRenderer> ();
    10.      wallMaterial = wallRenderer.material;
    11.      colPop = GetComponent<CollisionProperty> ();
    12. }
    13. void OnCollisionEnter(Collision other){
    14.      CollisionProperty cp = other.gameObject.GetComponent<CollisionProperty> ();
    15.      if (cp != null){
    16.          switch (cp.myProp)
    17.          {
    18.          case CollisionProperty.PropType.blue:
    19.              DoBlue (other.gameObject);
    20.              break;
    21.          case CollisionProperty.PropType.red:
    22.              DoRed (other.gameObject);
    23.              break;
    24.          case CollisionProperty.PropType.green:
    25.              DoGreen ();
    26.              break;
    27.          case CollisionProperty.PropType.yellow:
    28.              DoYellow();
    29.              break;
    30.          case CollisionProperty.PropType.goal1:
    31.              DoG1();
    32.              break;
    33.          case CollisionProperty.PropType.goal2:
    34.              DoG2();
    35.              break;
    36.          }
    37.      }
    38. }
    39. public void DoRed(GameObject destroyObject){
    40.      if (activated == false && (colPop.myProp != CollisionProperty.PropType.red)) {
    41.          activated = true;
    42.          Destroy (destroyObject.gameObject);
    43.          wallMaterial.color = Color.red;
    44.          colPop.myProp = CollisionProperty.PropType.red;
    45.          StartCoroutine (countdown ());
    46.      } else if (colPop.myProp == CollisionProperty.PropType.red) {
    47.          //add more points because it is the same color
    48.      } else {
    49.          activated= true;
    50.          Destroy (destroyObject.gameObject);
    51.          wallMaterial.color = Color.red;
    52.          //just changed color
    53.      }
    54. }
    55. public void DoBlue(GameObject destroyObject){
    56.      if (activated == false && (colPop.myProp != CollisionProperty.PropType.blue)) {
    57.          activated = true;
    58.          Destroy (destroyObject.gameObject);
    59.          wallMaterial.color = Color.blue;
    60.          colPop.myProp = CollisionProperty.PropType.blue;
    61.          StartCoroutine (countdown ());
    62.      } else if (colPop.myProp == CollisionProperty.PropType.blue) {
    63.          //add more points because it is the same color
    64.      } else {
    65.          activated= true;
    66.          Destroy (destroyObject.gameObject);
    67.          wallMaterial.color = Color.blue;
    68.          //just changed color
    69.      }
    70. }
    71. public void DoGreen(){
    72. }
    73. public void DoYellow(){
    74. }
    75. public void DoG1(){
    76. }
    77. public void DoG2(){
    78. }
    79. IEnumerator countdown()
    80. {
    81.      yield return new WaitForSeconds(5);
    82.      activated = false;
    83.      Debug.Log ("false");
    84. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    which lines are highlighted when you click on those errors, the code you've pasted in doesn't line up with the line numbers...

    after a quick look only thing that stands out is that the parameter's being passed into the "do###" functions are already gameobjects so
    Code (csharp):
    1. Destroy(destroyObject.gameObject);
    might be getting confused?


    other than that, you probably should be looking at passing the color of the object being collided with and make a single generic function rather than having the same thing over and over with just the color value as the difference between them
     
  3. grkpektis

    grkpektis

    Joined:
    Mar 24, 2016
    Posts:
    10
    The lines that are highlighted are

    if (activated == false && (colPop.myProp != CollisionProperty.PropType.red)) {
    if (activated == false && (colPop.myProp != CollisionProperty.PropType.blue)) {
     
  4. Toper

    Toper

    Joined:
    Feb 7, 2013
    Posts:
    2
    Are you sure that colPop was instantiated in Awake ?
    Put a break point on these lines and debug to see what Object is null, at the exact collision moment(OnCollisionEnter).
     
  5. grkpektis

    grkpektis

    Joined:
    Mar 24, 2016
    Posts:
    10
    I did, that works. Is it possible that destroying the object destroys the script and that's why it's not working
     
  6. grkpektis

    grkpektis

    Joined:
    Mar 24, 2016
    Posts:
    10
    Never mind I figured it out, if anyone is curious or has a similar problem

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CollisionAction : MonoBehaviour {
    6.  
    7.     bool activated;
    8.     int value;
    9.  
    10.     private MeshRenderer wallRenderer;
    11.     private Material wallMaterial;
    12.     private CollisionProperty colPop;
    13.     public enum WallColorMode {blue, red, yellow, green, white};
    14.     public WallColorMode colorMode = WallColorMode.white;
    15.  
    16.     void Awake (){
    17.         wallRenderer = GetComponent<MeshRenderer> ();
    18.         wallMaterial = wallRenderer.material;
    19.     }
    20.  
    21.     void OnCollisionEnter(Collision other){
    22.         CollisionProperty cp = other.gameObject.GetComponent<CollisionProperty> ();
    23.  
    24.         if (cp != null){
    25.             switch (cp.myProp)
    26.             {
    27.             case CollisionProperty.PropType.blue:
    28.                 DoBlue (other.gameObject);
    29.                 break;
    30.             case CollisionProperty.PropType.red:
    31.                 DoRed (other.gameObject);
    32.                 break;
    33.             case CollisionProperty.PropType.green:
    34.                 DoGreen (other.gameObject);
    35.                 break;
    36.             case CollisionProperty.PropType.yellow:
    37.                 DoYellow(other.gameObject);
    38.                 break;
    39.             }
    40.         }
    41.     }
    42.  
    43.  
    44.     public void DoRed(GameObject destroyObject)
    45.     {
    46.         if (activated == false && colorMode != WallColorMode.red)
    47.         {
    48.             activated = true;
    49.             Destroy(destroyObject);
    50.             wallMaterial.color = Color.red;
    51.             colorMode = WallColorMode.red;
    52.             StartCoroutine (Countdown ());
    53.         }
    54.         else if (colorMode == WallColorMode.red)
    55.         {
    56.             //add more points because it is the same color
    57.         }
    58.     }
    59.  
    60.     public void DoBlue(GameObject destroyObject)
    61.     {
    62.         if (activated == false && colorMode != WallColorMode.blue)
    63.         {
    64.             activated = true;
    65.             Destroy(destroyObject);
    66.             wallMaterial.color = Color.blue;
    67.             colorMode = WallColorMode.blue;
    68.             StartCoroutine (Countdown ());
    69.         }
    70.         else if (colorMode == WallColorMode.blue)
    71.         {
    72.             //add more points because it is the same color
    73.         }
    74.     }
    75.     public void DoGreen(GameObject destroyObject)
    76.     {
    77.         if (activated == false && colorMode != WallColorMode.green)
    78.         {
    79.             activated = true;
    80.             Destroy(destroyObject);
    81.             wallMaterial.color = Color.green;
    82.             colorMode = WallColorMode.green;
    83.             StartCoroutine (Countdown ());
    84.         }
    85.         else if (colorMode == WallColorMode.green)
    86.         {
    87.             //add more points because it is the same color
    88.         }
    89.     }
    90.  
    91.     public void DoYellow(GameObject destroyObject)
    92.     {
    93.         if (activated == false && colorMode != WallColorMode.yellow)
    94.         {
    95.             activated = true;
    96.             Destroy(destroyObject);
    97.             wallMaterial.color = Color.yellow;
    98.             colorMode = WallColorMode.yellow;
    99.             StartCoroutine (Countdown ());
    100.         }
    101.         else if (colorMode == WallColorMode.yellow)
    102.         {
    103.         //add more points because it is the same color
    104.         }
    105.     }
    106.  
    107.     IEnumerator Countdown()
    108.     {
    109.         yield return new WaitForSeconds(5);
    110.         activated = false;
    111.         Debug.Log ("false");
    112.     }
    113. }
    [/code]