Hey guys, my game involves cockroaches that die, and leave behind a corpse version of itself. The cockroach corpse gameobject has a simple script on it. This cockroach corpse gameobject also has the tag of “Corpse”.
function Start () {
SpawnTimer.roachCorpses ++;
}
function FadeDestroy(){
collider.enabled = false;
renderer.enabled = false;
yield WaitForSeconds(1);
renderer.enabled = true;
yield WaitForSeconds(.8); <----I can’t get this to not become a sunglasses smiley on this forum (supposed to be point eight)
renderer.enabled = false;
yield WaitForSeconds(.6);
renderer.enabled = true;
yield WaitForSeconds(.3);
renderer.enabled = false;
yield WaitForSeconds(.1);
renderer.enabled = true;
yield WaitForSeconds(.1);
renderer.enabled = false;
Destroy(gameObject);
}
FadeDestroy is simply an artistic deletion of the corpse. It flickers for a few seconds and then deletes itself from the scene. It is a function that I wish to be called when the variable roachCorpses goes above 3. I basically want no more than 3 corpses on the screen at any time for performance reasons.
So on another script that is not attached to the cockroach corpse gameObject, I have the following code:
static var roachCorpses : int = 0;
function Update()
{
if(roachCorpses>=4){
var Corpse = GameObject.FindWithTag(“Corpse”);
}
^^^^ Okay now that I’ve declared this variable Corpse to be a randomly-selected corpse gameObject, is there a good way of calling the FadeDestroy function within that specific gameObject that is stored in the Corpse variable? I’ve been having trouble doing this. Maybe there is a better way to be doing the same thing?
Cast the gameobject to a corpse object. Then use getcomponent to get a reference to the script on the corpse object that contains the function you want to call. Then use that reference to call the function.
Thanks for the replies. Could you give me an example of what casting the gameObject would look like? I dont understand what you mean by cast the gameobject to a corpse object. Could you maybe give me pseudocode? I read the getcomponent documentation but I’m not fully understanding how to use it. The website only gives examples of accessing other scripts on the same object.
The name of the script on the corpse gameObject is called CorpseFadeDestroy
The function that deletes the roach within that script is called FadeDestroy
var Corpse = GameObject.FindWithTag(“Corpse”);
var other : CorpseFadeDestroy = Corpse.GetComponent(CorpseFadeDestroy);
Corpse.other.FadeDestroy();
This code doesnt work, it’s just the last thing I ended up trying. Any help is tremendously appreciate.
var Corpse : Corpse = GameObject.FindWithTag("Corpse") as Corpse;
var other : CorpseFadeDestroy = Corpse.GetComponent(CorpseFadeDestroy);
other.FadeDestroy();
or this
var other : CorpseFadeDestroy = GameObject.FindWithTag("Corpse").GetComponent(CorpseFadeDestroy);
other.FadeDestroy();
When I tried your first suggestion, I got "The name ‘Corpse’ does not denote a valid type (‘not found’) in the first of the 3 lines (2 errors for each place the word Corpse is used)
I tried the 2nd one and i got NullReferenceException: Object reference not set to an instance of an object.
The line that brought this error was the
other.FadeDestroy();
I appreciate your responses! Do you have any more ideas on how to do this?