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 /
avatar image
0
Question by serenefox · Feb 16, 2014 at 02:46 AM · javascriptgetcomponentobject pool

Object pool with "Null Reference Exception" problem

Hello, I am trying to use an object pool for my pipes that will be coming into screen instead of instantiating them for better performance. I found a tutorial on youtube about object pools and changed the syntax to javaScript while changing everything to my own variables. The problem I am having is I get a "Null Reference Exception" because of these lines in my "SteamPipePool" Script:

    steamPipes[i].GetComponent("SteamPipeMove");
    steamPipeMove.Activate();

I think the Null reference is because I haven't been able to correctly setup my GetComponent to grab the script from each game object instantiated, but after trying multiple things I can't get it to work.

I have two scripts(one on the pipe Prefab and one on the Object pool manager in my scene). Currently I have my Update function in the "SteamPipePool" script enabled for testing with the mouse click.

If anyone could point me in the right direction I would be very grateful.


This is the script on my pipe prefab named "SteamPipeMove":

 var pipeLife : float = 3.0;
 var pipeSpeed : float = 10.0;
 private var timeToReturn : float = 0.0;
 
 function Start()
 {
     timeToReturn = Time.time + pipeLife;
 }
 
 function Update () 
 { 
     PipeMove();
     CountDown();
 }
 
 function PipeMove ()
 {
     transform.Translate(-pipeSpeed * Time.deltaTime, 0,0);
 }
 
 function Activate ()
 {
     timeToReturn = Time.time + pipeLife;
     transform.position = Vector3(11, 1, 0);
 }
 
 function Deactivate ()
 {
     gameObject.SetActive(false);
 }
 
 function CountDown ()
 {
     if(timeToReturn < Time.time)
     {
         Debug.Log("Hit");
         Deactivate();
     }
 }


And this is the script I have on my Object Pool Manager named "SteamPipePool":

 var moveTimeMin : float = 1.0;
 var moveTimeMax : float = 2.3;
 var pipe : Transform;
 var steamPipes : GameObject[] = null;
 var numberOfPipes : int = 6;
 private var steamPipeMove : SteamPipeMove;
 private var i : int;
 
 function Start()
 {
     steamPipes = new GameObject[numberOfPipes];
     InstantiatePipes();
 }
 
 function Update () 
 {
     //To test the whole thing
     if(Input.GetMouseButton(0) == true)
     {
         ActivatePipes();
     }
 }
 
 function InstantiatePipes()
 {
     for( i = 0; i < numberOfPipes; i++)
     {
         steamPipes[i] = Instantiate(pipe.gameObject,Vector3(13,1,0),Quaternion.identity)as GameObject;
         steamPipes[i].SetActive(false);
     }
 }
 
 function ActivatePipes()
 {
     for( i = 0; i < numberOfPipes; i++)
     {
         if(steamPipes[i].activeInHierarchy == false)
         {
             steamPipes[i].SetActive(true);
             steamPipes[i].GetComponent("SteamPipeMove");
             steamPipeMove.Activate();
             return;
         
         }
     }
 }

Eventually I want to alternate different pipes randomly instead of just one pipe but I think I need to get this working first.

Thanks for the help!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by highpockets · Feb 16, 2014 at 07:21 AM

try this on line 40:

 steamPipeMove = steamPipes[i].GetComponent("SteamPipeMove");
Comment
Add comment · Show 4 · 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 fafase · Feb 16, 2014 at 07:25 AM 0
Share

also,

 s$$anonymous$$mPipe$$anonymous$$ove = s$$anonymous$$mPipes[i].GetComponent(S$$anonymous$$mPipe$$anonymous$$ove);

is better.

avatar image serenefox · Feb 16, 2014 at 07:59 AM 0
Share

Yup that was it. Thanks you two. I thought I tried this earlier but I guess not, good thing I went back and tried it again with your suggestions. Thanks to you I can move on to working on Random.Range to send the pipes at different times and then different types of pipes. I am still kind of new to scripting so it will be mostly trial and error but now I can.

avatar image fafase · Feb 16, 2014 at 08:05 AM 1
Share

Since you are in a learning process, there is a more efficient pooling system, you are using an array which needs to find the object and find an unused object to activate, if you use stack you can skip that part since the object on the top is always ready to be used and you always stack on the top without looking for the object in the collection.

avatar image serenefox · Feb 16, 2014 at 04:30 PM 0
Share

Is their any documentation of this? Possibly with examples? I did a quick search on google and am only finding C# examples and not much related to unity. I tried searching the scripting reference but only got GL and I don't know if thats what you're talking about. Thanks for letting me know I will have to look more into later when I have more time.

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

19 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

Related Questions

ren]er cube problem 0 Answers

create GUI.Label and then access to it.. 2 Answers

Scripts not functioning or interacting. 1 Answer

Access a script that has NO class? 2 Answers

Get.Component from C# to Java 1 Answer


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