Search Unity

Referencing components in script

Discussion in 'Scripting' started by LinkinBark, Dec 17, 2019.

  1. LinkinBark

    LinkinBark

    Joined:
    Sep 21, 2019
    Posts:
    4
    I've been trying to make an object in my scene do certain things after it "Breaks"(Reaches 0 Health) Such as add a rigidbody to it and disable the SphereCollider for certain layers via script but it saying that the SphereCollider isn't referenced, i'm not really fluent in C# so it's very difficult trying to work this out by myself.
    here's the code if it'll help:​
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Damageable : MonoBehaviour
    6. {
    7.     public int maxHealth = 1000;
    8.  
    9.     public int defence = 200; //Intergrate at later date.
    10.     int currentHealth;
    11.     public string DestructionText; //Option to add Optional situational text via Inspector.
    12.     public Transform masterCollider; //Intergrate at later date.
    13.     public LayerMask interactables; //Intergrate at later date.
    14.     public bool Destroyable; //For Breakable Objects.
    15.     private bool cannotCollid = false;
    16.     SphereCollider b_Collider;//Referencing Sphere Collider on object script is attached too.
    17.     private void Start()
    18.     {
    19.         currentHealth = maxHealth;
    20.     }
    21.  
    22.     public void TakeDamage(int damage)
    23.     {
    24.         currentHealth -= damage;
    25.  
    26.         //Add Animation here add later date, Must be situational (Cont.)
    27.         // i.e being able to drag & drop different animations for different objects.
    28.  
    29.         if(currentHealth <= 0)
    30.         {
    31.             Break();
    32.         }
    33.     }
    34.  
    35.     public void Break() //or Die, adds Rigidbody as well as removes collider for player, NPCs and  other inportant moving objects for vanity purposes.
    36.     {
    37.         Debug.Log(DestructionText); //Text in console upon Death/Breaking
    38.         if(Destroyable == true)
    39.         {
    40.  
    41.             //gameObject.AddComponent(typeof(Rigidbody));
    42.             b_Collider.enabled = cannotCollid; //Disabling Collider, Will add layermask at later date when i know how to.
    43.         }
    44.     }
    45. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Copy and paste the actual error message, please, rather than trying to paraphrase it. Paraphrasing something you don't understand will almost always change it significantly, and presumably any question you post online is because you don't understand it, so just as a rule, always copy and paste the full error message. Error messages also contian other info like line numbers that are valuable.

    I can see in your code that you have declared b_Collider but have never assigned anything to it, so it's going to be null. Generally you'll want a line like this inside your Start() function:
    Code (csharp):
    1. b_Collider = GetComponent<SphereCollider>();
    Trying to use it will cause a NullReferenceException, which I'm taking a guess is what you've interpreted as "isn't referenced"? If so then this is exactly what I mean when I say that paraphrasing something you don't understand will change it significantly: A NullReferenceException doesn't mean that something wasn't referenced; it means that something whose value is null was referenced. Very much the opposite!

    (And if that's not the error message you got and rephrased, then it's still a good reason to paste the exact error message, because if it wasn't that then I don't know what it is and can't help you.)
     
    LinkinBark likes this.
  3. LinkinBark

    LinkinBark

    Joined:
    Sep 21, 2019
    Posts:
    4
    Thanks! i copy and pasted the code you posted and it works perfectly, lol such a simple solution
    I don't want to seem lazy but do you know how to make the spherecollider disable only for certain layers?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This will be generally done with the GameObject's Layers. You can configure the physics (in the physics settings panel) so that layer "some number" doesn't collide with whatever layers you don't want to collide with, then in the script, change gameObject.layer to "some number".
     
    LinkinBark likes this.
  5. LinkinBark

    LinkinBark

    Joined:
    Sep 21, 2019
    Posts:
    4
    You're a god send i cant even imagine how much time it would have taken to figure this out as a newbie.