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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Calling properties from other Script instances

Discussion in 'Scripting' started by gjfnasjdnasklnd, Jun 28, 2020.

  1. gjfnasjdnasklnd

    gjfnasjdnasklnd

    Joined:
    Jun 15, 2020
    Posts:
    8
    I have set up two cubes as children on my Character Controller to act as extra collision detectors. I don't think I'm creating instances properly? Both cubes have scripts on them, I need to access those properties from Character Controller script.

    Console error in Unity:
    NullReferenceException: Object reference not set to an instance of an object
    CapsuleController.checkCollision () (at Assets/Scripts/CharacterController.cs:56)
    CapsuleController.Update () (at Assets/Scripts/CharacterController.cs:50)

    On the main CharacterController script:
    (Edit: code updates as I try to fix this)
    Code (CSharp):
    1.     public class UpperCollisionScript : MonoBehaviour
    2.     {
    3.         public GameObject upperCollisionObject; //linked to objects in Unity Inspector
    4.         public GameObject lowerCollisionObject;
    5.    
    6.         void Update()
    7.         {
    8.             checkCollision();
    9.         }
    10.    
    11.         private void checkCollision()
    12.         {
    13.             Debug.Log("checkCollision() called"); //is called
    14.             int uLayer = lowerCollisionObject.GetComponent<UpperCollisionScript>().ULayer;
    15.             int lLayer = upperCollisionObject.GetComponent<LowerCollisionScript>().LLayer;
    16.             Debug.Log(uLayer); //nothing logged
    17.             Debug.Log(lLayer); //nothing logged
    18.  
    19.             if (uLayer == 9 && lLayer == 9) //checking layers the two objects collide with and changing player status
    20.             {
    21.                 status = MovementStatus.scale;
    22.             }
    23.             else if (uLayer == 9 && lLayer == 8)
    24.             {
    25.                 status = MovementStatus.normal;
    26.             }
    27.             else if (uLayer == 9 && lLayer != 9)
    28.             {
    29.                 status = MovementStatus.scale;
    30.             }
    31.         }
    32.    
    33.     }
    34. }
    And the scripts on the two objects are like so:

    Code (CSharp):
    1. public class UpperCollisionScript : MonoBehaviour
    2. {
    3.  
    4.     private int uLayer = 8; //groundLayer
    5.  
    6.     public int ULayer
    7.     {
    8.         get => uLayer;
    9.         set => uLayer = value;
    10.     }
    11.  
    12.     private void OnCollisionEnter(Collision other)
    13.     {
    14.         Debug.Log("upper collision occurred"); //does not get called on collision
    15.         uLayer = other.gameObject.layer;
    16.     }
    17.  
    18.     private void OnCollisionExit(Collision other)
    19.     {
    20.         uLayer = 8; //groundLayer
    21.     }
    22. }
    The upper and lower collision scripts are on the components, the component is called within the script in Awake() so I don't know why OnCollisionEnter is not executed and debugs are not showing in console.
     
    Last edited: Jun 28, 2020
  2. gjfnasjdnasklnd

    gjfnasjdnasklnd

    Joined:
    Jun 15, 2020
    Posts:
    8
    has all the answers I need.