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

AddComponent returns null

Discussion in 'Scripting' started by howaboutno000, Nov 28, 2020.

  1. howaboutno000

    howaboutno000

    Joined:
    Nov 13, 2020
    Posts:
    20
    Tag: Untagged
    Layer: Default
    Code (CSharp):
    1.  
    2. namespace x
    3. {
    4.     public class testCube : MonoBehaviour
    5.     {
    6.  
    7.         void Awake()
    8.         {
    9.             var cl = gameObject.AddComponent<Collider>();
    10.             if (cl == null)
    11.             {
    12.                 Debug.Log("Is null");
    13.             }
    14.             else
    15.             {
    16.                 cl.isTrigger = true;
    17.                 cl.contactOffset = 100.0f;
    18.             }
    19.         }
    20.    
    21.         void OnTriggerEnter(Collider _collider)
    22.         {
    23.             Debug.Log($"CollEnter");
    24.         }
    25.  
    26.         void OnTriggerExit(Collider _collider)
    27.         {
    28.             Debug.Log($"CollExit");
    29.         }
    30.  
    31.     }
    32. }
    Created a default cube, then attached the script above.
    After pressing play I got: "Is null". Why? How? What can I do about it?

    Time.scaleTime is 1. Even if I remove standard collider it still isn't there. Moving it to Start() doesn't change it either.
     
    Last edited: Nov 28, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    I don't think you're supposed to add a "Collider" to objects; it's a base class that is inherited by various specific kinds of Collider, like BoxCollider, SphereCollider, etc. Try adding a specific kind of collider instead?
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Always check your console! If you try
    AddComponent<Collider>()
    , you'll get following message in there, explaining why you get null back:
    That's also why you want to always keep your console free of warnings and errors, even if they don't break things, otherwise it's easy to miss important messages like this.
     
    Bunny83 and Kurt-Dekker like this.
  4. howaboutno000

    howaboutno000

    Joined:
    Nov 13, 2020
    Posts:
    20
    var cl = gameObject.AddComponent<SphereCollider>();
    works and is not null anymore, but the Colliders still don't react. Neither
    void OnTriggerEnter(Collider other)
    nor
    void OnTriggerEnter(SphereCollider other)


    Those are two GameObjects with the same script which are placed in seperate location, then I moved one cube into another cube and nothing happened.
     
  5. Stoicheia

    Stoicheia

    Joined:
    Jun 25, 2020
    Posts:
    27
    Remember, a trigger cannot collide with another trigger. Since you are placing the same script on both objects and the script sets the collider to be a trigger in Awake(), I believe this is your problem.
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @howaboutno000

    For Physics collisions to work, you'll also have to have a Rigidbody in one of the colliding objects.

    So if you have a collider or collider set as a trigger in a prop object, then your player should have a collider and a Rigidbody, otherwise OnTriggerEnter / OnCollisionEnter won't register anything.
     
    howaboutno000 and Bunny83 like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    This ^ ^ ^
     
  8. howaboutno000

    howaboutno000

    Joined:
    Nov 13, 2020
    Posts:
    20
    That did it, why won't Unity throw an error if there's a collider without rigidbodies, are there any other usages to the colliders?

    Anyways, thank you.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    The terrain.

    The floor.

    The ceiling.

    The walls.

    etc.

    Not everything moves in a Physics game.
     
    eses and Bunny83 like this.