- Home /
can't access child script
This is my hierarchy panel: I have this script attached to my dmgCube gameObject in the hierarchy and I would like to access the Flame gameObject. I tried almost everything within my current skills :(
public class DmgCube_scr : MonoBehaviour {
Color originalColor;
GameObject target;
GameObject lastTarget;
GameController_scr gc_scr;
GameObject trickCube;
public bool dmgCharged;
Cube_scr cube_scr;
ParticleEmitter staffLight;
GameObject flame;
void Awake()
{
trickCube = GameObject.Find ("trickCube");
}
void Start () {
originalColor = trickCube.renderer.material.color;
lastTarget = trickCube;
staffLight = gameObject.transform.parent.GetComponentInChildren<ParticleEmitter>();
// flame = gameObject.transform.parent.transform.Find("Flame").gameObject;
// I need to have this part fixed...help pls.
Debug.Log ("flame = " + flame.name);
DeChargeStaff();
}
void Update () {
// ::::::::::::::::::::::::::::::::::::::::::::::: TARGETING :::::::::::::::::::::::::::::::::::::::::::
Ray targetingRay = new Ray(transform.position, transform.TransformDirection (Vector3.forward));
RaycastHit targetHit;
if(Physics.Raycast (targetingRay, out targetHit, 1f))
{
if(targetHit.transform.gameObject.tag == "cube")
{
target = targetHit.transform.gameObject;
cube_scr = target.GetComponent<Cube_scr>();
//Debug.Log ("target name = " + target.name);
MarkTarget ();
lastTarget = target;
}
}else{
if(lastTarget != null)
{
lastTarget.renderer.material.color = originalColor;
}
else{lastTarget = trickCube;}
target = null;
cube_scr = null;
}
if(Input.GetKeyDown (KeyCode.O))
{
ChargeStaff ();
}
if(Input.GetKeyDown (KeyCode.I))
{
DeChargeStaff ();
}
}
public void ChargeStaff()
{
staffLight.enabled = true;
dmgCharged = true;
}
public void DeChargeStaff()
{
staffLight.enabled = false;
dmgCharged = false;
}
void MarkTarget()
{
if(target != lastTarget)
{
target.renderer.material.color = Color.gray;
lastTarget.renderer.material.color = originalColor;
}
}
// ::::::::::::::::::::::::::::::::::::::::::::::: ATTACKING :::::::::::::::::::::::::::::::::::::::::::
public void Attack()
{
if(dmgCharged && target != null)
{
StartCoroutine (CoAttack ());
}
}
public IEnumerator CoAttack()
{
yield return new WaitForSeconds(0.35f);
Destroy (target);
lastTarget = trickCube;
DeChargeStaff();
cube_scr.CubeExplode();
}
}
Answer by robertbu · Oct 23, 2014 at 04:43 PM
Try this:
flame = transform.parent.Find("staff/Flame").gameObject;
You have to specify a path for Transform.Find(). If 'Flame' as a unique script, then you could also do:
flame = transform.parent.GetComponentInChildren<SomeClass>().gameObject;
thanks, man. Could you please explain to me why can't I turn those torches (particle emitters) ON and OFF via my ChargeStaff and DeCHargeStaff functions?
use the ParticleSystem ins$$anonymous$$d of the ParticleEmitter. ParticleSystem has functions to clear particles and start/pause/stop the system.
I solved it via parent gameObject.SetActive(false) of the emitter. But thanks for the advice, I'll keep that in $$anonymous$$d.
Answer by smoggach · Oct 23, 2014 at 04:40 PM
There are a variety of ways, the slowest of which being your atempt (GameObject.Find).
The fastest way is to use a straight up public reference.
public GameObject flameObject;
Now your dmgCube should have a new property. Drag the Flame game object into the new property.
The next best way is to set your Flame object to have a tag, say "Flame" http://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
flame = GameObject.FindWithTag("Flame");
this is not a good idea, since I have 10 mages in my scene and each of them has this script on his own dmgCube gameObject. I need to access that particular mage's Flame object from that particular mage's dmgCube object.
Then it makes sense that each mage should have a script that keeps track of all the children he needs references to. You should set the reference when you create the Flame object anyways. It's always better to set references when you create things than to go looking for them later.