Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 07, 2013 at 07:55 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by Machineman1357 · May 07, 2013 at 05:39 AM · raycasttimerboolean

How to make a timer that loops with booleans?

I have 3 scripts: Melee script is a raycast that shoots 5 meters out and when it hits something it calls the gameObject's function "PickItemUp". The OrangeScript initiates the timer afterwards and "picks up" the orange. The OrangeSpawnerScript then starts a timer from 5 seconds and when it reaches 0 it spawns a new orange and should be able to loop. This is where I am having problems as when I first start the game the timer goes down and an orange spawns and I can "pick it up", but then afterwards the boolean that enables the timer does NOT enable itself again. I can MANUALLY set it to true through the inspector however, but my script won't do it by itself. Here are the scripts:

using UnityEngine; using System.Collections;

public class Melee : MonoBehaviour {

 public float maxDistance = 5.0F;
 
 void Update () 
 {
     if(Input.GetKeyDown(KeyCode.E))
 {
     RaycastHit hit;
     if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), out hit))
     {
         float hitDistance = hit.distance;
         if(hitDistance < maxDistance)
         {
             hit.transform.SendMessage("PickUpItem", SendMessageOptions.DontRequireReceiver);
         }
     }
 }
 }

}

using UnityEngine; using System.Collections;

public class OrangeScript : MonoBehaviour {

 public OrangeSpawnerScript OSScript;
 
 public void PickUpItem ()
 {
     OSScript.Timer();
     Destroy(gameObject);
 }
 
 
 

}

using UnityEngine; using System.Collections;

public class OrangeSpawnerScript : MonoBehaviour {

 public Transform theOrange;
 public float timer = 5.0F;
 public bool timerEnabled;
 
 public void Timer ()
 {
     timerEnabled = true;
 }
 
 void Update ()
 {
     if(timerEnabled)
     {
         timer -= 1 * Time.deltaTime;
         if(timer <= 0)
         {
             Instantiate(theOrange, transform.position, transform.rotation);
             timerEnabled = false;
             timer = 5.0F;
         }
     }
 }
 

}

Thanks so much in advance :)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · May 07, 2013 at 07:55 AM 0
Share

Do not do anything like this at all.

be sure to use Invoke() and InvokeRepeating() for simple timers in Unity.

there are 100s great questions on here about it, and, unityGE$$anonymous$$S.com for many articles.

In your case it would be something like InvokeRepeating("makeAnOrange", 5, 5);

it's just that simple.

to stop it happening, "InvokeCancel("makeAnOrange");"

to start it again InvokeRepeating("makeAnOrange", 5, 5);

first search please, cheers

avatar image Machineman1357 · May 09, 2013 at 01:33 AM 0
Share

Ins$$anonymous$$d of telling me the obvious path to success why don't you help me out and actually show me the location of these so called 100's of questions? I'll wait while you find me some; please hurry.

1 Reply

  • Sort: 
avatar image
0

Answer by bodec · May 07, 2013 at 06:27 AM

on your first script you need to access your spawn script and reset the Boolean to start your timer again. You could also reset it if(no child exist)then spawn. (not real code but idea).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Controlled Automatic Fire Rate (RayCasting) 2 Answers

Player can't block door 0 Answers

How to stop the countdown timer when I have collected all the coins? 2 Answers

How Do I check if a Bool was True a few moments ago 2 Answers

determine the minimum time for the player to hold the button 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges