- Home /
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");
}
}
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!
Thank you so much!!! I'm happily taking down these pesky bombers now. I never would have thought of that!!
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.
Your answer
Follow this Question
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