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 /
avatar image
0
Question by W1k3 · Nov 10, 2014 at 09:02 AM · c#objectupdatetimer

Is an object with an update function possible? (C#)

If I have a class, and In another script, I create an instance of that class, is the update function usable?

Nothing I put in it seems to run.

The class:

 using UnityEngine;
 using System.Collections;
 
 public class MM_Timer {
 
     float delayy;
     float time;
     public bool tik = false;
     bool randmode;
     float min;
     float max;
     bool paused = false;    
 
     void Update () {
         if (time < delayy && tik == false && paused == false)
         {
             time += Time.deltaTime;
         }
         else if (!paused)
         {
             tik = true;
             if(randmode){
                 delayy = Random.Range(min, max);
             }
         }
     }
     public bool tick(){
         if (tik){
             tik = false;
             return true;
         }
         else{return false;}
     }
 
     public void start(){paused = false;}
     public void puase(){paused = true;}
 
 
     public MM_Timer(float delay, bool autostart){
         delayy = delay;
         if (!autostart) paused = true;
     }
     public MM_Timer(float minDelay, float maxDelay, bool autostart){
         min = minDelay;
         max = maxDelay;
         if (!autostart) paused = true;
     }
 }
 

Just in case, my Script:

 using UnityEngine;
 using System.Collections;
 
 public class bow : MonoBehaviour {
 
     public GameObject arrow;
     public Transform shotpos;
     bool canfire = true;
     public static float firerate = 0.5f;
     MM_Timer timer = new MM_Timer (firerate, true);
     
     void Update () {
         if(Input.GetButton("Fire1")){
             if(canfire){
             Instantiate(arrow, shotpos.position, transform.rotation);
                 canfire = false;
             }
 
         }
         if(timer.tick()){
             canfire = true;
         }
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by CHPedersen · Nov 10, 2014 at 09:09 AM

Update is a built-in method defined in class MonoBehaviour. It gets called automatically by Unity's runtime only in classes which derive from MonoBehaviour (we call those 'scripts'). These are the ones that can be attached to GameObjects.

You are free to define whatever code you like in any other object you like and call that as you see fit. But if it doesn't inherit (derive) from MonoBehaviour, Unity will not call those standard functions for you automatically. Given a reference to an object of type MM_Timer, you would have to simply call its Update method yourself.

You could call it inside the Update of a script that does inherit from MonoBehaviour, and couple it to Unity's framerate that way, if you wished.

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 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 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

How to destroy in an amount of time 1 Answer

Distribute terrain in zones 3 Answers

For loop resetting itself, but needs to stop 2 Answers

Multiple Cars not working 1 Answer

Convert this string formatting from C# to JS 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