Getting a Bool to a child from another child. (Semi Solved)

I’m trying to access a child script from another child’s script off the same parent. Basically I want to have it so if the gameobject runs out of health it won’t be able to move any more. My script is

using UnityEngine;
using System.Collections;

public class SkeletonAggro : MonoBehaviour {

public GameObject playersoldier;
Transform playertransform;
float runspeed = 3;
public bool fighting = false ;
public bool foundplayer = false;
private skelattack SkelAttack;
private AttackingEnemy attackingenemy;

void Awake ()
{
attackingenemy = GetComponentInChildren ();
SkelAttack = GetComponentInParent ();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == “Player” && foundplayer == false) {
playersoldier = other.gameObject;
playertransform = playersoldier.transform;
fighting = true;
foundplayer = true;
SphereCollider planetCollider = GetComponent ();
planetCollider.radius = .3f;

}

if (other.gameObject.tag == “Blocker”)
{
fighting = false;
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject.tag == “Blocker”)
{
foundplayer = false;
SphereCollider planetCollider = GetComponent ();
planetCollider.radius = 4f;
}

}

void Update ()
{
if (fighting == true && attackingenemy.alive == true)
{
transform.parent.LookAt (playertransform);
transform.parent.position += transform.parent.forward * runspeed * Time.deltaTime;
SkelAttack.running = true;
}

}

}

However I keep on getting an error “Object reference not set to an instance of an object” and the error message directs me to the line "if (fighting == true && attackingenemy.alive == true) ". Anyone know why this is happening and what I can do to fix it?

Well I figured out a get around, I made another parent script that got reference to the alive bool. From there I got the child to get a reference from that new script. If anyone though as a way how I could have done this by accessing the child script with alive on it, please let me know. I’m trying to learn as much as I can.

why not keep the movement in the same gameobject as the health script? seems the best idea to me…

but if you dont want that you can either send a reference from the parent to each children (assuming you create them at runtime, otherwise you can just add a reference in the editor…) or make the children search the reference to the child he wants to disable

1 Like

Just make a public member of the script component you need to access

public SomeScriptName someScript;

so it’ll show up in the Inspector, then go to the inspector and drag the other GameObject into it. It’ll automatically set the component on that GameObject to the variable (provided a component of that type exists), so you can access it from within the code.

Also, use Code tags when you post code. Pretty pretty please?

1 Like

Thanks dworm and Lysander for the info. To dworm I really should have done that, not really sure why I didn’t but that’s why I’m doing this to learn. And to Lysander yes I’ll use them from now on, I didn’t know how to use them last night when I posted this, but I looked it up now since I now know the name. :smile: