- Home /
Figured it out, check comments
Setting an object from a pool active not working
Hi,
I am trying to pull one of my "Explosion" Prefabs from an object pool that was instantiated at start and set it to active when my player runs into my dynamite character. But for some reason I can't get it to work. I have tried two different ways. Here is the first version of my script for the first way:
using UnityEngine;
using System.Collections;
public class DynaDerpExplode : MonoBehaviour
{
private GameObject explosionPrefab;
private PlayerHealth playerHealth;
public int damage = 2;
private GameObject objectPool;
// Use this for initialization
void Start ()
{
objectPool = GameObject.Find ("ObjectPool");
explosionPrefab = objectPool.GetComponentInChildren<Explosion>();
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
Explode();
other.gameObject.GetComponent<PlayerHealth>().playerCurHp -= damage;
}
}
void Explode()
{
explosionPrefab.transform.position = gameObject.transform.position;
explosionPrefab.SetActive(true);
gameObject.SetActive(false);
}
}
But I get this error:
cs(16,69): error CS0246: The type or namespace name `Explosion' could not be found. Are you missing a using directive or an assembly reference?
Then I tried this as my second attempt:
using UnityEngine;
using System.Collections;
public class DynaDerpExplode : MonoBehaviour
{
private GameObject explosionPrefab;
private PlayerHealth playerHealth;
public int damage = 2;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
Explode();
other.gameObject.GetComponent<PlayerHealth>().playerCurHp -= damage;
}
}
void Explode()
{
explosionPrefab.transform.position = gameObject.transform.position;
explosionPrefab.SetActive(true);
gameObject.SetActive(false);
}
}
But I get a null reference exception when the player hits the dynamite character and it points to this line:
explosionPrefab.transform.position = gameObject.transform.position;
If anyone has an idea of what I can do to get this to work please let me know, thanks!
Ok I figured out the problem. I had to use:
$$anonymous$$yPoolScript.instance.GetObjectForType("name of prefab in pool script", true);
//Then use Gameobject.Find
//Do what I want with it
I had to reference the Object pool Script I was using that deactivated it.
Answer by highpockets · Feb 24, 2014 at 02:36 AM
You need to set the explosionPrefab to an instance of an object.
In the Start function, put this:
explosionPrefab = GameObject.Find("ExplosionPrefabNameInHierarchy");
Now it is set to an instance of an object.
This is for the second attempt script that you posted
Thanks for the reply but I tried that too and I still get a null reference exception, could it be because the prefab I am trying to find is instantiated as inactive? Can the "Find" find it if its inactive?
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Null reference exception 1 Answer
Issues with instantiated prefabs 1 Answer
audio source not playing the audio clip 2 Answers