Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Rafa1996 · Sep 04, 2020 at 07:27 PM · instantiategetcomponentinchildren

GetComponentInChildren doesn't work when instantiating the object

So I have an enemy with a health bar. I have a script that manages enemy health. In the Start function I use GetComponentInChildren to get the Slider component. When I place the enemy in the scene and hit play, it works normally. But when instantiating the enemy object, which is how I want to do it, it doesn't work; the slider variable remains null. The weirdest part is that it worked before in other enemies using the same health script, but I can't figure out why it doesn't work with this one.

The enemy object is set up like this:alt text

And this is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class EnemyHealth : MonoBehaviour
 {
     public int maxHealth;
     public int health;
     public Slider healthSlider;
     public bool Hit;
     // Start is called before the first frame update
     void Start()
     {
         healthSlider = GetComponentInChildren<Slider>();
         health = maxHealth;
         healthSlider.maxValue = maxHealth;
         Hit = false;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         healthSlider.value = health;
     }
     public void TakeDamage(int damage)
     {
         health -= damage;
        
     }
 }

Comment
Add comment · Show 5
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 BenWiller1989 · Sep 04, 2020 at 08:01 PM 0
Share

You know that this is almost useless right? Please paste the variables and your image doesn't show up. If you need a fix, provide people with information.

avatar image Rafa1996 BenWiller1989 · Sep 04, 2020 at 08:15 PM 0
Share

I added the rest of my script. The image showed the hierarchy of the object. I have the parent object, which contains the script. That object has a canvas as its child, which has a slider as it child. I need to access the slider component from the script in the parent object.

Do you need more information?

avatar image BenWiller1989 BenWiller1989 · Sep 04, 2020 at 08:59 PM 0
Share

Good job for now! If you say, it worked on other once, but not on this object, maybe you have some failures in your hierarchy. I tried it out and it worked for me. That's why it would be good to have some Screenshots. You can do some things though, just to be sure. Replace the Enemy prefab with a new one and maybe restart unity.

avatar image Rafa1996 BenWiller1989 · Sep 04, 2020 at 09:09 PM 0
Share

I tried that and it still doesn't work. The script works when I just place the object in the scene. But not when instantiated. The object is being instantiated from another object with this script:

  using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class ShamanGoblin : $$anonymous$$onoBehaviour
     {
         public float spawnTime;
         public float summonTime;
         public bool isPresent;
         public string[] summonName;
         public int summonType;
         public bool enemiespresent;
         Animator anim;
         // Start is called before the first frame update
         void Start()
         {
             spawnTime = Random.Range(0.5f, 2f);
             anim = GetComponent<Animator>();
             summonTime = 3f;
         }
     
         // Update is called once per frame
         void Update()
         {
             spawnTime -= Time.deltaTime;
             if (spawnTime <= 0)
             {
                 spawnTime = 30f;
                 if (!isPresent)
                 {
                     anim.SetTrigger("Spawn");
                     isPresent = true;
                 }
             }
             if (isPresent && !enemiespresent)
             {
                 summonTime -= Time.deltaTime;
             }
             if (summonTime <= 0 && !enemiespresent)
             {
                 anim.SetTrigger("Attack");
                 
                 summonType = Random.Range(0, summonName.Length);
                 summonTime = Random.Range(2f, 4f);
                
             }
                if (transform.childCount == 0)
             {
                 enemiespresent = false;
             }
         }
     
         void SpawnEnemies()
         {
             print("Summon");
             GameObject summon = Instantiate(Resources.Load("Prefabs/" + summonName[summonType]) as GameObject);
             summon.transform.position = transform.position + new Vector3 (0, 3, 0);
             summon.transform.SetParent(transform);
             enemiespresent = true;
         }
     }





I can provide screenshots of whatever you may need.

avatar image BenWiller1989 · Sep 05, 2020 at 11:39 AM 0
Share

Forget about the bullshit I told you. I was wrong. I think my unity was broken or something. It's probably something else. We have to search more.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Sep 05, 2020 at 07:25 AM

Author the reference to the slider using a public variable in the inspector and get rid of GetComponentInChildren - will guarantee the problem is solved.

Barring your taking that approach, are you sure you are attaching the script to the top level object and not the slider?

Comment
Add comment · 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

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

240 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 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 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

GetComponentInChildren doesn't work when instantiating object 1 Answer

The thing you want to instantiate is null 1 Answer

Instantiate a particle system, probably a stupid question 2 Answers

Object reference not set message, directly after instantiation 1 Answer

How can I increase the speed gradually of each instantiated clone. 0 Answers


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