- Home /
Help, Missing Object
Hi everyone, I've been stuck on this part for a while and I don't know why, but I'm trying to clone my collect GameObject when isCollect is true. Then when isCollect is false, it should destroy the clone, but it's not doing that, its destroying the entire GameObject I think, and then when I take a look in the inspector it says Missing Object. I look through so many forms and questions and answers about this and I seem to have no luck. Any suggestions??
if (isCollect) {
if (!collectorObject) {
collectorPos = player.transform.position;
collectClone = Instantiate (collect, collectorPos, transform.rotation) as GameObject;
collectorObject = true;
}
superCollectTimer += Time.deltaTime;
if (superCollectTimer < 5) {
Debug.Log ("Collecting");
} else {
if (superCollectTimer >= 4.5f) {
superCollectTimer = 0;
isCollect = false;
collectorTexture.enabled = false;
energy -= 3;
}
}
}
if (isCollect == false) {
Destroy(collectClone);
}
I don't detect a glaring issue, but all these conditionals look a wee bit messy. It looks like a case for a simple state machine or at least some coroutines to clean things up.
What says "missing object" - does it only say this after this code executes? Are you ever writing to or changing the variable which holds your prefab? This is generally unwise unless you know exactly what you're doing.
Yea sorry about that I have conditions functioning with other things. But it says missing object with the collectClone after its been excuted. And nope, I'm not changing the variable.
$$anonymous$$y thought is that isCollect is being set to false immediately before execution returns to the line which destroys collectClone if it's false.
Yes isCollect starts off as being set to false, but once a certain score is obtained it is set to true. Once it is set to true the collectClone object appears. Then when the timer runs out ((superCollectTimer >= 4.5f)) isCollect is set to false and the collectClone should be destroyed. hopefully that makes since. Do you think that's the problem? And if so how do you think I can fix it?
Any other suggestions from anyone, Im can't seem to figure out whats wrong.
Answer by games4ever · Mar 12, 2015 at 07:59 PM
Try this...
void Awake() {
collectClone = null;
}
void Update() {
if (isCollect) {
if (collectClone == null)
{
collectorPos = player.transform.position;
collectClone = Instantiate (collect, collectorPos, transform.rotation) as GameObject;
}
else
{
superCollectTimer += Time.deltaTime;
if (superCollectTimer < 5)
{
Debug.Log ("Collecting");
}
else
{
Destroy(collectClone);
collectClone = null;
superCollectTimer = 0;
isCollect = false;
collectorTexture.enabled = false;
energy -= 3;
}
}
} }
I was looking at referencing nulls, I didnt know how to implement it in my script but I will try this soon.