Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

138 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

Related Questions

PlayFab CloudScript NullReferenceException 0 Answers

Endless Runner Game: Turn Corners 0 Answers

Move from A to B after recieving touch input 1 Answer

How do I disable 2D camera follow when my player collides with a trigger object? 0 Answers

How to know if a game object is attached or is being used by another game object or script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges