Hello all, I am building obstacles, these two example obstacles could be controlled by my script:
- Scenario One - The player triggers a collider and a door object is deactivated, the player has a certain amount of time to get to the door before it closes (the object is re-activated).
- Scenario Two - Similar to scenario one but instead of a door object being deactivated, a bridge object is activated and then the player has an amount of time to cross the bridge before it is deactivated.
I am trying to write a script to run this obstacle in my game, the script has two states, one for each Scenario to make the script have multiple uses that are basically the same (I need my state bool to be changeable within the inspector panel):
- State 1 (When “activateObjectInstead”
is false) - Deactivate object, wait
for (time) seconds, then activate
object. - State 2 (When “activateObjectInstead”
is true) - Activate object, wait for
(time) seconds, then deactivate the
object.
I’d like the state of a bool to be checked and that would decide what state/scenario the script would follow. I have written a script however it seems that both my IF statements get activated, here is my script:
#pragma strict
//State 1 (When "activateObjectInstead" is false) - Deactivate object, wait for (time) seconds, then activate object.
//State 2 (When "activateObjectInstead" is true) - Activate object, wait for (time) seconds, then deactivate the object.
var object : GameObject; //The object that will be activated or deactivated by the script.
var controller : GameObject;
var buttonmodel : GameObject;
var time : int;
var activateObjectInstead : boolean; //The bool that decides what state the script will run.
var soundOne : AudioClip;
var soundTwo : AudioClip;
public var soundvolume : float = 0.5;
function OnTriggerEnter(collision : Collider) {
if (collision.gameObject.tag == (controller));
{
if (activateObjectInstead == false); //Only if "activateObjectInstead" is false.
{
object.SetActive(false); //Deactivate the object.
buttonmodel.SetActive(false);
AudioSource.PlayClipAtPoint(soundOne, transform.position, soundvolume);
Debug.Log("Set Inactive");
yield WaitForSeconds (time); //Wait for (time) seconds.
object.SetActive(true); //Reactivate the object.
buttonmodel.SetActive(true);
udioSource.PlayClipAtPoint(soundTwo, transform.position, soundvolume);
Debug.Log("Set Active");
}
if (activateObjectInstead == true); //Only if "activateObjectInstead" is true.
{
object.SetActive(true); //Activate the object.
buttonmodel.SetActive(false);
AudioSource.PlayClipAtPoint(soundTwo, transform.position, soundvolume);
Debug.Log("Set Inactive");
yield WaitForSeconds (time); //Wait for (time) seconds.
object.SetActive(false); //Deactivate the object.
buttonmodel.SetActive(true);
AudioSource.PlayClipAtPoint(soundOne, transform.position, soundvolume);
Debug.Log("Set Active");
}
}
}