- Home /
Make gameobject inactive and then after sometime make it active again
Player will collect the radical gameobject. After that they will need to select the correct option. If they select the wrong answer, i will have to make the hit.gameObject.active = true; the object did reappear but when player try to collide with the gameobject, hit.gameObject.active = false is not working. I feel that my logic quite weird, but i not sure how to tackle the question.
if(hit.gameObject.tag == "rad1b")
{
//Destroy(hit.gameObject);
hit.gameObject.active = false;
//radicals[1].active = false;
// play a sound clip at the exact position we hit the object
AudioSource.PlayClipAtPoint(hitSound, transform.position);
//networkView.RPC("removeDiamond", RPCMode.AllBuffered);
rad1b = true;
print("rad1b");
}
So basically the reactivate of gameObject is done at the openableDoor.js script. And i didn't do anything to the player script.
function showChoices()// attached to the gameObject door
{
if(choice)
{
if(wrong)
GUI.Label (Rect (20, 30, 300, 60), "Wrong Option!");
if(MoveAround.rad1a)
{
//~ // You may put a label to show a message to the player
if(GUI.Button(Rect(20,44,100,120), R01a, ""))
{
audio.PlayOneShot(correctSound);
print("rad01a");
correct = true;
}
}
if(MoveAround.rad1b)
{
if(GUI.Button(Rect(125,44,100,120), R01b, ""))
{
audio.PlayOneShot(wrongSound);
print("rad01b");
wrong = true;
//R01bb.active = true; // Set gameobject to active
}
}
function Update(){
if(wrong)
{
if(MoveAround.rad1a) // if this true mean that it already destroy, need to respawn the object
{
radical[0].active = true;
r1a = true;
}
if(MoveAround.rad1b)
{
radical[1].active = true;
r1b = true;
}
if(MoveAround.rad1c)
{
radical[2].active = true;
r1c = true;
}
if(MoveAround.rad1d)
{
radical[3].active = true;
r1d = true;
}
if(MoveAround.rad1e)
{
radical[4].active = true;
r1e = true;
}
if(MoveAround.rad1f)
{
radical[5].active = true;
r1f = true;
}
}
if(wrong)
{
MoveAround.rad1a = false;
MoveAround.rad1b = false;
MoveAround.rad1c = false;
MoveAround.rad1d = false;
MoveAround.rad1e = false;
MoveAround.rad1f = false;
}
}
*Edit the code snippet
Answer by Meltdown · Mar 20, 2012 at 06:49 AM
MissyPooh I'd suggest using a co-routine with a time delay to reactivate your object. It really is quite simple to use, follow the instructions/code samples on the linked page.
function Update() { StartCoroutine(reloadObject()); }
function reloadObject() { hit.gameObject.active = true; }
it is something like that??
Answer by aldonaletto · Mar 20, 2012 at 02:57 PM
The problem doesn't seem to be in the code you've posted. Maybe another part of the code isn't being reset when you re-activate the object. Could the variable rad1f be the culprit? If you don't set it to false again, some other part of the code will "think" the radical was already collected.
hello, i have add in more code snippet. Would appreciate if you could take alook what is wrong with the code snippet. Basically the gameObject did reappear. Just that when the player going to collide with it the second time, it does not do this (hit.gameObject.active = false)
The code has some weird things: GUI.Button, GUI.Label and any other GUI functions must be used in the OnGUI event, but they are in showChoices - unless you call showChoices in OnGUI, you will get lots of Null Reference errors. Another weird thing: Update seems to be declared inside showChoices - is it a typo?