- Home /
Enable and Disable a plane
Okay, so, recently i made a script to enable an image when i hit it's collider. The script's function was to pause the game, and show the image. If the player pressed "P", then the game would unpause and the image would not be there anymore.
So, as i am making a FPS game and i wanted to change my current combat system, i was thinking "why not?" so i kinda updated the script to to the following:
I wanted to make my script DISABLE a plane (that is a muzzleflash) when the player started the game, and then ENABLE when the player pressed the left mouse button, wait for let's say 1 second and then disable it again. This would make a flash screen, and it would be easy to make.
But i am stuck! I don't know what i did wrong, but it simply doesn't do anything. It sucessfuly disable the plane in the start of the game, but it doesn't enable and disable it like a flash when i press the left mouse button =(
Here's the script (Java)
#pragma strict
var MuzzeFlash : GameObject; //The plane OR muzzeflash.
public var FireRate : float; //A variable to determine the time to wait before disabling it.
function Start () //This function will make the quad be disabled in the start of the scene.
{
MuzzeFlash.SetActive (false);
}
function CoUpdate(): IEnumerator //The only thing i could find that would fit was this.
{
if (Input.GetButton("Fire1")) { //On left mouse click:
MuzzeFlash.SetActive (true); //Enable the plane?
yield WaitForSeconds (FireRate); //Wait for the seconds in the variable.
MuzzeFlash.SetActive (false); //Disable the plane?
}
}
Answer by SmomoGame · Oct 20, 2016 at 04:45 AM
function Update()
{
if (Input.GetButtonDown("Fire1")) { //On left mouse click:
CoUpdate() ;
}
}
function CoUpdate(){
MuzzeFlash.SetActive (true); //Enable the plane?
yield WaitForSeconds (FireRate); //Wait for the seconds in the variable.
MuzzeFlash.SetActive (false); //Disable the plane?
}
Do not call coroutine every frame, or these coroutines will execute at same time, and the plane will act weirdly.
Use GetButtonDown instead.
But I think the point is that CoUpdate function NEVER execute because you NEVER call it.
CoUpdate not means "This is a coroutine version of Update function". Just a custom function. By the way, Update CANNOT be coroutine.
In javascript, you don't need to add IEnumerator, compiler will do it for you.
It kinda works. I said kinda because it is completely ignoring my FireRate variable and it disable the muzzle flash only 4 seconds after even if i change it, but i am going to accept your answer because it solved the main issue.
Your answer
Follow this Question
Related Questions
How do I enable my scripts on default?? 3 Answers
Disable FirstPersonController.cs when I click on a object, then re-enable it when I hit Q 1 Answer
How to make Animation and Physics work together? 0 Answers
What the variable motorSetter and motor type should be ? 1 Answer
How can i add a second camera that will be showing only specific gameobject in game window ? 1 Answer