- Home /
Script activate after amount of time
I have been working on a project where I faint after a certain amount of time. I used the space bar to activate the faint but I am having trouble finding anything that i can understand. Can someone show me a link or a pointer on how to do this. (i am a beginner to unity and scripting). Thank you.
Answer by Professor Snake · Jan 20, 2013 at 06:23 PM
You can use the following code to delay something:
Fainting.js
var delay:float;
function Faint(){
var timePointer:float=Time.time+delay;
for(;timePointer>Time.time;)
yield;
/fainting code here.
}
Answer by sparkzbarca · Jan 20, 2013 at 06:22 PM
http://unitygems.com/mistakes1/
make something happen in the future, coroutines and invoke header
mark as answered and have a nice day.
Answer by Kortekk · Jan 20, 2013 at 06:28 PM
Look into how "coroutines" are used.
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html
You can use "yield" statements to delay the execution of code.
As an example, you could start with something like:
void Update()
{
if (Input.GetKeyDown("space"))
{
StartCoroutine("Faint");
}
}
IEnumerator Faint()
{
print("Disable something here");
yield return new WaitForSeconds(5);
print("Enable something here");
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Best place to learn Marching Cubes Algorithm? 2 Answers
Make an object move when clicking a GUI button? 1 Answer
Need Help - Last Problem Converting PC to Android - Using JS to read CS 2 Answers