- Home /
How to access a script on a instantiated prefab
So I have a SpawnManager that is already in the scene view. And after 50 kills it spawns a boss prefab. I want to be able to change the boss prefab script variables with my SpawnManager script.
For example. In my SpawnManager script I say that if the boss has less than 50 health, make text appear.
I already tried GameObject.Find and GetComponent. But I always get the Object reference not set to an instance of an object error.
Thanks in advance.
Answer by Collect · Oct 28, 2018 at 06:24 PM
I highly suggest avoiding this completely by attaching the health, and the check for the bosses health to the boss itself, rather than being managed by an outside source.
That being said, your issue is a typical problem of not setting a reference to your instantiation, because instead of trying to find the created boss, it's trying to find the prefab.
To fix this make sure that you're setting the reference for the created boss upon instantiation. Now, you can access the GetComponents, like your boss script, from the reference.
The reason I can't add it to the boss is because when the boss dies a coroutine has to start. And when the boss dies the GameObject is destroyed. I don't get an error anymore but the coroutine doesn't start
(I know that the code is messy)
$$anonymous$$y UImanager code:
public void Update()
{
GameObject spawn_$$anonymous$$anager = GameObject.Find("Spawn_$$anonymous$$anager");
Spawn$$anonymous$$anager spawnmanager = spawn_$$anonymous$$anager.GetComponent<Spawn$$anonymous$$anager>();
if (livesImageDisplay.sprite == lives[0])
{
score = 0;
}
if (score >= 5)
{
if (spawned == false)
{
GameObject spawnedBoss = Instantiate(bossBakx, new Vector3(7.7f, transform.position.y), Quaternion.identity);
bossBakxScript = bossBakx.GetComponent<Boss>();
spawned = true;
spawnmanager.bossSpawned = true;
}
}
if (bossBakxScript.bossHealth <= 0)
{
StartCoroutine(illBeBakx());
}
$$anonymous$$y boss Code:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Laser")
{
bossHealth = bossHealth - 5;
Debug.Log(bossHealth);
Destroy(other.gameObject);
if (bossHealth <= 0)
{
GameObject[] book;
book = GameObject.FindGameObjectsWithTag("BakxBook");
foreach (GameObject Book in book)
{
Destroy(Book);
}
GameObject spawn_$$anonymous$$anager = GameObject.Find("Spawn_$$anonymous$$anager");
Spawn$$anonymous$$anager spawnmanager = spawn_$$anonymous$$anager.GetComponent<Spawn$$anonymous$$anager>();
spawnmanager.bossSpawned = false;
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}
Thanks again
I'm not sure what the coroutine llBeBakx(); does, but it could possibly be placed in your Spawn$$anonymous$$anager, and run there. That way even after the boss dies your coroutine continues.
So I did what you said and placed the coroutine in the spawnmanager and the text appears but it doesn't disappear.
Here is my coroutine:
public IEnumerator illBeBakx()
{
Debug.Log("Point A");
GameObject.Find("I'll be bakx.").transform.localScale = new Vector3(1, 1, 1);
yield return new WaitForSeconds(2);
Debug.Log("Point B");
GameObject.Find("I'll be bakx.").transform.localScale = new Vector3(0, 0, 0);
}
(The Debug.Log is only to see if it works. And only point A is printed) The I'll be bakx. is just a textobject in the Canvas
Thanks for the quick responses.