Search Unity

Script returning Null half of the time (?)

Discussion in 'Scripting' started by SomerenV, Oct 22, 2020.

  1. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    Here's the situation:
    I've got four gameobjects, each with a skidmark script (Asphalt, Sand, Grass and Snow).
    I've got a GameObject, named Ground Surface Master, with a script attached that handles different surface types. I can set the amount of surfaces and for each surface set the friction, audio and attach gameobject that have a skidmark script. Currently there are 4 surface types.


    On surfaces I attach a Ground Instance script that allows me to select a surface type, based on the surface types created in the Ground Surface Master.

    So far so good. I can get the correct surface type with debugging so the script is working. Tire collides with a surface and Debug.Log tells me which surface it collided with. However, if I do the same for the skidmarks it won't work. For two surface types it tells me what the corresponding skidmark is, but for the other two it's Null and I've got no clue why it's doing that.

    This is what I'm using for the Ground Surface Master:
    Code (CSharp):
    1.     public class GroundSurfaceMaster : MonoBehaviour
    2.     {
    3.         public GroundSurface[] surfaceTypes;
    4.         public static GroundSurface[] surfaceTypesStatic;
    5.  
    6.  
    7.         void Start()
    8.         {
    9.             surfaceTypesStatic = surfaceTypes;
    10.         }
    11.     }
    12.  
    13.     //Class for individual surface types
    14.     [System.Serializable]
    15.     public class GroundSurface
    16.     {
    17.         public string name = "Surface";
    18.         public float friction;
    19.         public Skidmarks skidmark;
    20.         [Tooltip("Always leave tire marks")]
    21.         public bool alwaysScrape;
    22.         [Tooltip("Rims leave sparks on this surface")]
    23.         public AudioClip tireSnd;
    24.     }
    This is what I'm using for the Ground Surface Instance:
    Code (CSharp):
    1. public class GroundSurfaceInstance : MonoBehaviour
    2.     {
    3.         public int surfaceType;
    4.         [System.NonSerialized]
    5.         public float friction;
    6.         public Skidmarks skidmark;
    7.  
    8.         void Start()
    9.         {
    10.                 friction = GroundSurfaceMaster.surfaceTypesStatic[surfaceType].friction;
    11.                 skidmark = GroundSurfaceMaster.surfaceTypesStatic[surfaceType].skidmark;
    12.         }
    13.     }
    I use this code on the wheels of my vehicle to detect if it's hitting a GameObject with GroundSurfaceInstance attached:
    Code (CSharp):
    1.             if (Physics.Raycast(transform.position, -Vector3.up, out hit) && hit.collider.gameObject.GetComponent<GroundSurfaceInstance>())
    2.             {
    3.                    Debug.Log(hit.collider.gameObject.GetComponent<GroundSurfaceInstance>().skidmark);
    4.                     Debug.Log(Hit.collider.gameObject.GetComponent<GroundSurfaceInstance>().surfaceType);
    5.  
    6.             }
    So it successfully detects the surface type (0, 1, 2 and 4) but only detects two types of Skidmarks being used and returns a Null for the other two even though every surface type is set up correctly. Anyone that has any clue as to what is going on? Also, with this script I'm not yet using the skidmarks script(s), only detecting which one is attached to the surface the wheel currently is on.