Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TonicMind · Jun 12, 2016 at 06:31 PM · inheritanceclassesnew

Create a new script object from a class which inherits from MonoBehavior

I just want to create a new script object. I need for its Update() function to be called at the very least. What must I do to make this happen?

I've tried Timer inputUseTimer = new Timer(); but then the Update() function never gets called, I've tried typing in Instantiate() but its wrong because that function doesn't take that kind of argument (my Timer class).

Again, how?

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

4 Replies

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

Answer by Bonfire-Boy · Jun 13, 2016 at 12:44 AM

Create Monobehaviours using AddComponent, don't use constructors.

Timer inputUseTimer = (new GameObject()).AddComponent();

Comment
Add comment · Show 1 · 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
avatar image TonicMind · Jun 13, 2016 at 04:01 PM 0
Share

For future readers, what I'm going to do is just attach my Timer class as a component to each gameobject which needs a Timer. I accepted this one as the answer because Eric replied first with a good answer, and other ideas weren't possible.

avatar image
2

Answer by CountCurls · Jun 13, 2016 at 04:11 AM

In case Bonfire Boy's answer isn't clear I've added some code as an example

 using UnityEngine;
 using System.Collections;
 
 //This would be the main object that you want to attach the timer to
 public class MainObject : MonoBehaviour {
 
     public Timer inputUseTimer ;
 
     // Use this for initialization
     void Start () {
         timer = gameObject.AddComponent<Timer>();
         timer.InitialiseValues(/* Your arguments */);
     }
 }
 
 
 /***********************************************/
 
 using UnityEngine;
 using System.Collections;
 using System;
 
 //This is the Timer object
 public class Timer : MonoBehaviour {
 
     void Update () {
         //Do stuff
     }
 
     public void InitialiseValues(/*Your arguments*/)
     {
         //Assign the values you need
     }
 }
 
 /****** End *************/

gameObject.AddComponent<>() will create a new instance of the script and attach it to the object that called it. When it comes to using Instantiate() you used that for prefabs and gameobjects stored as fields to create clones of them.

Comment
Add comment · Show 3 · 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
avatar image TonicMind · Jun 13, 2016 at 02:45 PM 0
Share

These are all really great answers. I was hoping for something much cleaner though. Something where I wouldn't have to declare a separate object variable like another gameobject just to attach a Timer script object to.

Would it be possible to forgo the inheriting of monobehaviour and just use some .net calls to achieve this? The only reason I wanted to use $$anonymous$$onoBehaviour was it would call Update() automatically and that way I could update the time there and check to see if the timer was done.

Is that possible?

avatar image CountCurls · Jun 13, 2016 at 03:15 PM 0
Share

It's hard to suggest what to do without some more information on what exactly it is you want to do. But you don't have to create another gameObject to use the time, ins$$anonymous$$d you can attach the timer to whatever gameObject it is that needs it (but having the timer as a separate gameObject would mean that other gameObjects would be able to use it without unnecessary coupling).

I wouldn't recommend ditching $$anonymous$$onoBehaviour and using .Net. If the timer is just there to send out notifications then I'd suggest using the observer design pattern (Java example: https://www.youtube.com/watch?v=wiQdrH2YpT4).

Going with this route would allow the Timer object to make the checks every Update cycle, and any class that implements the appropriate interface would be able to subscribe to or observe it. Then whenever a check meets the requirement you can notify all the observers by calling a method on them.

I don't know if that's overkill for what you want to do or not, but I use the observer pattern all the time and it helps prevent classes needlessly relying on each other.

avatar image TonicMind CountCurls · Jun 13, 2016 at 03:58 PM 0
Share

The thing is I created this "Timer" class to be something I can just create (instantiate, create using new keyword, etc.) so I don't have to keep doing things like the single line of code in the original question. I thought it would be nice to encapsulate it and make it a bit less esoteric.

But I supposed I could attach this class as a component to a gameobject that needs it. Sounds like a good plan... I tend to overthink things.

avatar image
1

Answer by lzjamaoge · Jun 13, 2016 at 06:08 AM

You can not new a component object by youselft. Update function called by GameObject. Componet shoud be handled by GameObject instances. So would be like this

 GameObject obj = new GameObject();
 obj.AddComponent<T>();

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
avatar image
0

Answer by Brijs · Jun 13, 2016 at 06:51 AM

You can do this :

1) Make a Monobehaviour in which declare a static delegate or event and attach monobehaviour to GameObject.

2) Invoke that delegate in Update().

3) In Timer(not monobehaviour) class constructor register your method to delegate or event.

4) You are ready to make object(Instantiate) of timer class in any other script.

Now you can run your code in every update from Timer class

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

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

Inheriting from classes from other files (Javascript) 1 Answer

How to make an array inherit from a class? 2 Answers

Cannot use "new" keyword on class inheriting from MonoBehaviour 2 Answers

Defining and inheriting from a Javascript Class 0 Answers

Class extends Object but does not inherit variables 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