- Home /
How to create. The most basic flashlight possible.
I'm making a survival horror game, and I'm trying to make it realistic. I'm using a spotlight parented to a 1st person controller. No physical flashing is used. I need to figure out a way to add a five minute timer to the spotlight (it's a short game) so once that time is up, you just wander around in the ark until you die. No extra batteries, no picking up the flashlight, just a spotlight timer, or any simple alternative. Anyone got any ideas I can use? edit Got it. I apologize for sounding so stupid, for whatever reason, everything I tried wasn't working. Almost all of my coding experience is in HTML, so it's like learning a new language.
to make timers in Unity you use "Invoke" - it's very simple
Answer by Adam-Buckner · Aug 02, 2012 at 07:53 AM
How are you turning on and off your light? Or have you set it the light's "enabled" state to "on" in the inspector?
If you look at the documentation for "light":
http://docs.unity3d.com/Documentation/ScriptReference/Light.html http://docs.unity3d.com/Documentation/Components/class-Light.html
When reading these pages, you should find information about "enabled" (which is turning a component "on" or "off" by either clicking the box in the inspector or setting the state to "true" or "false" in code.
An example of the code used would be:
var myLight : Light;
myLight.enabled = false;
For a timer, you should read up on "Time" in the docs:
http://docs.unity3d.com/Documentation/ScriptReference/Time.html
With this you can create a timer. I would suggest, for simplicity, updating the time counter in the "Update()" function.
And example of the code used would be:
var myTimer : float;
myTimer = myTimer + Time.deltaTime;
BUT: All of this being said... this is relatively trivial when it comes to doing things in Unity. This says to me that you could use a little more grounding in Unity and using the Unity engine. For that I would suggest a few things:
Visit the teaching section of the Forum and run a few of the tutorials listed there.
Visit the teaching section of the Forum and take one of the classes I present for Unity Technologies (free)
Visit the teaching section of the Forum and read up on "Getting Started with Coding for Unity"
Read the first page on the scripting reference and the basic subjects you find on the main page: http://docs.unity3d.com/Documentation/ScriptReference/index.html
All of these will give you a much better foundation in Unity and the Unity engine. Things like this will become easy, if not trivial.
Angel, everything you say is correct and he should do what you say. However one thing...
"...create a timer. I would suggest, for simplicity, updating the time counter in the "Update()" function..."
that is not correct, Angel. For a timer, just use "invoke". It is extremely simple.
Invoke("TurnOffLight", 300); // turn off the light in 300 seconds
function TurnOffLight()
{
// turn off the light here
}
that's it - the only way to do it.
"Invoke" is the main and basic command used in program$$anonymous$$g video games. $$anonymous$$any video games are simply long lists of hundreds of Invoke commands.
Yes, Fattie, you are correct. I was trying to keep the scope of the answer to something so basic that it was using the most common and familiar methods. Because so much code can go into Update(), and learning to increment a value is important, I chose Update() as my suggestion.
Looking up Invoke() is a good, tho' slightly more advanced, topic: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html
Your answer
Follow this Question
Related Questions
Unity and Playmaker - Flashlight Decrease Logic 0 Answers
How do you write a Pauseable-Timer Script in Unity JavaScript? 1 Answer
Flashlight timer 2 Answers
Flash light timer 1 Answer
Timer that stops at trigger 1 Answer