- Home /
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?
Answer by Bonfire-Boy · Jun 13, 2016 at 12:44 AM
Create Monobehaviours using AddComponent, don't use constructors.
Timer inputUseTimer = (new GameObject()).AddComponent();
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.
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.
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?
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.
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.
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>();
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
Your answer
Follow this Question
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