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:
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;
}
}
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.
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?
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.
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.
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.
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?
Your answer
Follow this Question
Related Questions
GetComponentInChildren doesn't work when instantiating object 1 Answer
Using constructors in Unity (C#) 8 Answers
Performance of instantiate and destroy for bullets 2 Answers
Instantiate objects around a sphere 2 Answers
More problems with arrow shooting 0 Answers