• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Trashboat95 · Mar 07, 2018 at 06:07 PM · scripting problemscript.nullreferencinggame object

Help with adding variables to other script

Hey all!

RECAP: Why is this returning null and how can I fix it? To my knowledge its properly referenced.

LONG VERSION: I've been at this for a while now, and I can't seem to find the root of my problem. In the game I'm creating, you intercept bombers by shooting their engines out. Pretty simple right? I have 2 scripts, one on the bomber itself named BomberBrain and one on it's engines, BomberEngineHealth. What I want to happen is when an engine is shot, it adds +5 to the fallSpeed inside BomberBrain, so it slowly drifts downwards the more engines are shot. The problem is whenever I shoot out an engine, I immediately get "Failed to fall".... meaning the fallSpeed is never added to the BomberBrain's fallSpeed variable.

 public class BomberBrain : MonoBehaviour {
 
     public float speed = 0.1f;
     public float fallSpeed = 0.0f;
     public GameObject explosion;
 
     public Animation anim;
     private BomberEngineHealth bomberEngineHealth;
 
     void Start () {
 
         bomberEngineHealth = GetComponent<BomberEngineHealth>();
     }
 
     void Update() {
         //fly the bomber forwards
         transform.Translate(Vector3.forward * Time.deltaTime * speed);
         //bring 'er down
         transform.Translate(Vector3.down * Time.deltaTime * fallSpeed);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Ground")
         {
             Instantiate(explosion, gameObject.transform.position, gameObject.transform.rotation);
             Destroy(gameObject, 0.1f);
             Debug.Log("All hands lost!");
         }
     }
 }

And here is the BomberEngineHealth script

 public class BomberEngineHealth : MonoBehaviour {
 
     public float engineHealth = 50f;
     public GameObject explosion;
     public bool hasExploded;
     public GameObject smokeHolder;
     private BomberBrain bomberBrain;
 
     // Use this for initialization
     void Start () {
 
         smokeHolder.SetActive(false);
 
         bomberBrain = GetComponent<BomberBrain>();
 
     }

     public void TakeDamage(float amount)
     {
         engineHealth -= amount;
         if (engineHealth <= 0f)
             {
                 Die();
                 Fall();
         }
     }
 
     void Die()
     {
         
         if (!hasExploded)
         {
             Instantiate(explosion, gameObject.transform.position, gameObject.transform.rotation);
             hasExploded = true;
 
             smokeHolder.SetActive(true);               
         }    
         Destroy(gameObject, 0.1f);
     }
 
     void Fall()
     {
         if (bomberBrain != null)
         {
             Debug.Log("Fall");
             bomberBrain.fallSpeed += 5;
         }
         else Debug.Log("Failed to fall");
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MacDx · Mar 07, 2018 at 06:16 PM

The problem is how you're trying to get the reference.

You're using this

 bomberBrain = GetComponent<BomberBrain>();

However you said that BomberEngineHealth is on the engines and not on the same game object BomberBrain is attached to. GetComponent will only return components that are attached to the same game object the script calling it is attached to. Your BomberBrain (I'm assuming) is on a parent game object. So what you should do is call GetComponentInParent instead. Like this:

 bomberBrain = GetComponentInParent<BomberBrain>();

Hope this helps!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Trashboat95 · Mar 07, 2018 at 06:19 PM 0
Share

Thank you so much!!! I'm happily taking down these pesky bombers now. I never would have thought of that!!

avatar image
2

Answer by Tsequier · Mar 07, 2018 at 06:16 PM

Hey ! First of all, are all of those scripts on the same object ? Because when calling GetComponent() like you call it , Unity will try to find the component on the same object. Even if your BomberBrain script is on a parent or a child, your script won't be able to find it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i use a script to automatic place invisible walls around the terrain edges ? 1 Answer

how can I change a light with multiple triggers. ? 0 Answers

How can i make the camera to rotate smooth to next target how to move to next target and how to slowly stop before each target ? 1 Answer

How can i first destroy twice the objects then to create twice the objects ? 1 Answer

How can i find the height of a gameobject ? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges