- Home /
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!
Answer by highpockets · Feb 16, 2014 at 07:21 AM
try this on line 40:
steamPipeMove = steamPipes[i].GetComponent("SteamPipeMove");
also,
s$$anonymous$$mPipe$$anonymous$$ove = s$$anonymous$$mPipes[i].GetComponent(S$$anonymous$$mPipe$$anonymous$$ove);
is better.
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.
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.
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
Follow this Question
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