- Home /
Missing (Game Object)
Hellom, people. Ive got a small problem with my code that I can not solve. I'm trying to create a new AI to my roach and ive got a small problem with destroying the blood and instantiating it after. At the beggining the roach instantiates the blood but then when the blood destroys itself the GameObject var that it's value is the bloods prefab just shows " Missing (Game Object) and then it just wont instatiate the blood.
Heres the roachs AI :
using UnityEngine;using System.Collections;
public class AI_roach : MonoBehaviour
{
public float distenemy;
public bool combat=false;
public float movespeed =5;
public float rotationSpeed= 5;
public float cooldown=0;
public float hittype=0;
public bool spotted = false;
public float health = 100;
public GameObject bloodfab;
public GameObject enemy;
void Start()
{
GameObject bloodfab = GameObject.FindGameObjectWithTag("bloodt");
}
void Awake()
{
animation["walk"].speed = 1;
animation["attack2"].speed = 0.7f;
animation["attack1"].speed = 0.7f;
}
void Update()
{
GameObject bloodfab = GameObject.FindGameObjectWithTag("bloodt");
distenemy = Vector3.Distance(enemy.transform.position,transform.position);
if(combat && character_stats.health>0)
{
animation.Stop("walk");
Combat();
}
if(distenemy>3 && spotted)
{
combat=false;
WalkToEnemy();
}
else if(distenemy<=3 && spotted)
{
animation.Stop("walk");
combat=true;
}
else
Rest();
if(distenemy<10)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(enemy.transform.position - transform.position), rotationSpeed *Time.deltaTime);
if (!Physics.Linecast (transform.position, enemy.transform.position)&& !spotted)
{
spotted=true;
}
}
}
void WalkToEnemy()
{
transform.position+=transform.forward * movespeed * Time.deltaTime;
animation.Play("walk");
}
void Rest()
{
animation.Play("idle");
}
// _____________COMBAT_________________
//_____________________________________
//_____________________________________
void Combat()
{
if(cooldown>0)
cooldown-=1*Time.deltaTime;
if(cooldown<=0)
{
hittype=Random.Range(0,4);
if(hittype==3)
ThrowHit();
if(hittype<3)
RegularHit();
}
if (!animation.isPlaying)
animation.Play("idle");
if(character_stats.health<=0)
combat=false;
}
void RegularHit()
{
animation.Play("attack2");
if(distenemy<3)
{
bloodfab = Instantiate(bloodfab, enemy.transform.position , enemy.transform.rotation) as GameObject;
character_stats.health-=Random.Range(3,15);
}
cooldown=1.5f;
}
void ThrowHit()
{
animation.Play("attack1");
if(distenemy<3)
{
bloodfab = Instantiate(bloodfab, enemy.transform.position , enemy.transform.rotation) as GameObject;
character_stats.health-=Random.Range(12,31);
char_movesys.moveDir.y=15;
}
cooldown=3;
}
}
Ive tried to set the bloods GameObject var to the bloods prefab at Update method but it still shows "Missing (Game Object)". It's like the prefab removes it'self temporaly from unity untill the game stops. Please help me out.
Answer by dannyskim · Dec 05, 2011 at 08:36 PM
The easiest thing to do would be to create a private variable that is a placeholder for your bloodprefab object. You have your variable declaration:
public GameObject bloodfab;
so what you can do is have another variable declaration:
private GameObject bloodfabInstance;
and change your Instantiation to:
bloodfabInstance = Instantiate(bloodfab, enemy.transform.position , enemy.transform.rotation) as GameObject;
Granted, this is the quickest and probably dirtiest way to fix your issue, but it will get the job done.
Technically, when the public GameObject variable gets destroyed and you get a Missing GameObject exception reference, this is proper behavior inside of the Unity Engine. You're destroying that instance of the variable inside of memory when Destroy() is called. Because of this and the way Instantiations and Destroys occur, it has become fairly normal practice to use a Pool Manager system to preload several instances of your GameObject that are held in an array and managed by a singleton when you first load the level. This alleviates memory allocation and reallocation, which if kept unchecked will eventually lead to excessive Garbage Collection and stutters in your frame rate. If you're not familiar with arrays or standard List types, you should learn about them and understand their concepts and usage as soon as possible.
Also, declaring the variable in Update to fix this issue would actually be the worse thing to attempt, because that's wasted processing allocation.
Answer by benk0913 · Dec 06, 2011 at 12:08 PM
Ive already tried that, it didn't work. )= The place-holder calls itself 'Missing' too. I think it's because of the particle system in the prefab but I can not be sure. And redeclaring it at the update didn't work so I removed it but thanks for the tip, please help me out with this problem it holds me for a few days in the same position with that case.
Answer by benk0913 · Dec 07, 2011 at 11:31 AM
I think I didn't get you correctly ccause it still does the same.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can I use AddComponent to add (this.script) to an object? 1 Answer
AI control C# 1 Answer
Why doesn't this AI script work? c# 1 Answer