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 /
  • Help Room /
avatar image
1
Question by pzmq · Aug 29, 2016 at 07:52 PM · scripting problem

how do i Find an Object in the same hierarchy?

alt text

How do i Find the HealthBar i can't use bar = transform.find().getcomponent< BarScript >();

Player

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Player : MonoBehaviour {
 
     [SerializeField]
     private Stat health;
 
     private void Awake()
     {
         //health.initialize();
         
     }
     private void Start()
     {
         health.initialize();
         
 
     }
     
     // Update is called once per frame
     void Update () {
         
         if (Input.GetKeyDown(KeyCode.Q))
         {
             health.CurrentVal -= 10;
         }
         if (Input.GetKeyDown(KeyCode.W))
         {
             health.CurrentVal += 10;
             
         }
         if (Input.GetKeyDown(KeyCode.Z))
         {
             health.MaxVal -= 10;
         }
         if (Input.GetKeyDown(KeyCode.X))
         {
             health.MaxVal += 10;
 
         }
     }
 }
 

Stat

 using UnityEngine;
 using System.Collections;
 using System;
 
 [Serializable]
 public class Stat
 {
     [SerializeField]
     private BarScript bar;
 
     [SerializeField]
     private float maxVal;
 
     [SerializeField]
     private float currentVal;
 
     public float CurrentVal
     {
         get
         {
             return currentVal;
         }
 
         set
         {
             this.currentVal = Mathf.Clamp(value, 0, MaxVal);
             bar.Value = currentVal;
         }
     }
 
     public float MaxVal
     {
         get
         {
             return maxVal;
         }
 
         set
         {
             maxVal = value;
             bar.MaxValue = maxVal;
 
         }
     }
 
     public BarScript Bar
     {
         get
         {
             return bar;
         }
 
         set
         {
             bar = value;
         }
     }
 
     Transform FindChild(string name, Transform t)
     {
         if (t.name == name)
             return t;
 
         foreach (Transform child in t)
         {
             Transform ct = FindChild(name, child);
             if (ct != null)
                 return ct;
         }
         return null;
     }
 
     public void initialize()
     {
         this.MaxVal = maxVal;
         this.CurrentVal = currentVal;
 
     }
 }
 

sem-titulo.png (327.1 kB)
Comment
Add comment · Show 11
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 mj321 · Aug 29, 2016 at 08:03 PM 1
Share

Something like this might work:

 transform.parent.gameObject.GetComponentInChildren(...)
avatar image pzmq · Aug 29, 2016 at 08:41 PM 0
Share

How so? What should i put in the ... it isn't Barscript?

avatar image mj321 pzmq · Aug 29, 2016 at 08:58 PM 0
Share

Yes. The ... were supposed to mean "you know what to insert here" :)

avatar image pzmq mj321 · Aug 30, 2016 at 10:42 AM 0
Share

yh but when i put the $$anonymous$$eyWord BarScrpit it says that it is not a valid System.Type

Show more comments
avatar image mj321 pzmq · Aug 30, 2016 at 12:14 PM 0
Share

So do you want to find HealthBar or a component on HealthBar? In the latter case, can you show its components?

avatar image pzmq mj321 · Aug 30, 2016 at 05:30 PM 0
Share

I want do find a component that is a script called BarScript that is on the HealthBar

Show more comments
avatar image mj321 pzmq · Aug 30, 2016 at 06:56 PM 0
Share

I really thought something like this would have worked:

 BarScript script = transform.parent.gameObject.GetComponentInChildren<BarScript>();

I don't know why it doesn't, i would have to try it out myself.

$$anonymous$$aybe you can just add a public variable for the Healthbar to the player script and drag it in there in the editor like Unity wants us to do :)

avatar image ValkaiIcai · Sep 02, 2016 at 01:44 AM 0
Share

Couldn't you just try creating a GameObject variable, healthBar, and then referencing it to get the component?

 public GameObject healthBar;
 
 healthBar.GetComponent<BarScript>();
 
 
 //more code
avatar image ValkaiIcai · Sep 02, 2016 at 01:53 AM 0
Share

Or you can use

GameObject.FindGameObjectWithTag("HealthBar").GetComponent();

if you use this way, then don't forget to tag your health bar as "HealthBar"

0 Replies

· Add your reply
  • Sort: 

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

81 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

Related Questions

Easy Short kill score script not working 0 Answers

How do I make a x move and y move parameter 0 Answers

Problem when I call fonction with child class 1 Answer

Adding a Highscore to game 1 Answer

Help! Script won't work. 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