Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by PaxForce · Oct 23, 2014 at 04:33 PM · referencehierarchy

can't access child script

This is my hierarchy panel: alt text 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();
 
     }
 
 
 
 }

hierarchy.jpg (11.7 kB)
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
0
Best Answer

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;
Comment
Add comment · Show 3 · 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 PaxForce · Oct 23, 2014 at 05:16 PM 0
Share

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?

avatar image smoggach · Oct 23, 2014 at 05:19 PM 0
Share

use the ParticleSystem ins$$anonymous$$d of the ParticleEmitter. ParticleSystem has functions to clear particles and start/pause/stop the system.

avatar image PaxForce · Oct 23, 2014 at 06:40 PM 0
Share

I solved it via parent gameObject.SetActive(false) of the emitter. But thanks for the advice, I'll keep that in $$anonymous$$d.

avatar image
0

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");
Comment
Add comment · Show 3 · 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 PaxForce · Oct 23, 2014 at 04:46 PM 0
Share

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.

avatar image smoggach · Oct 23, 2014 at 04:57 PM 0
Share

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.

avatar image PaxForce · Oct 23, 2014 at 05:14 PM 0
Share

THAT I don't know how to do :o/ ...

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

28 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

PDF scripting reference? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Following a rigid body's position in same hierarchy 1 Answer

Referencing booleans from settings 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