Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 24, 2014 at 03:36 AM by serenefox for the following reason:

Figured it out, check comments

avatar image
0
Question by serenefox · Feb 24, 2014 at 12:48 AM · c#prefabnull reference exceptionobject pool

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!

Comment
Add comment · Show 1
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 serenefox · Feb 24, 2014 at 03:36 AM 0
Share

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.

1 Reply

  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · 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
avatar image serenefox · Feb 24, 2014 at 02:50 AM 0
Share

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

Answers Answers and Comments

20 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

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


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