Start and Update not called when creating instance of a class

Hi All,

I am designing a buff system and I decided to use a seperate timer class. Problem is, when I create an instance of the clas, Update nor Start are activated. I have no idea why it does not work. Anyone?

public class BuffObject extends MonoBehaviour
{
	var timer : int;
	var targetBuff : Buff;
	var targetSoldier : Soldier;
	
	//constructor
	function BuffObject(dur : int, buf : Buff, sol : Soldier)
	{
		Debug.Log("Creating buffobject!");
		timer = dur;
		targetBuff = buf;
		targetSoldier = sol;
	}
	
	function Start()
	{
		Debug.Log("Starting Timer!");
	        StartCoroutine(ActivateBuffTimer());
	}
	//set the timer value to a new value
	function ResetTimer(t : int)
	{
		timer = t;
	}
	
	//the buff timer coroutine
	function ActivateBuffTimer()
	{	
		targetSoldier.AddRemoveBuff(targetBuff, true);
		Debug.Log("Buf started!");
		while(timer > 0)
		{
			timer -= Time.deltaTime;
			yield;
		} 
		Debug.Log("Buf ended!!");
		//once the timer hits zero, remove the buff
		targetSoldier.AddRemoveBuff(targetBuff, false);
	}
}

Covering the very basics first… is your BuffObject scene object definitely active in the scene? If the object is not active in the scene, or the actual BuffObject component is not enabled, then the script will not be run, hence Start() and Update() not being called.

It is not a gameObject itself. It is something that is created as soon as a buff is cast on a unit.

What I want is to have a timer that I can adjust while it is running. Since I have tons of possible buffs, I figured it would be best if I made a seperate instance of each timer and add them to a list. I can then easily reference to a specific timer in that list.

Edit:

I think I am going to try it in a different way. Maybe attaching a timer to the GameObject that contains the particlesystem for the graphics of the buff is a better idea.

If you’ve created an instance of that class, then it should exist as a component on an object somewhere in your scene. The Start() and Update() functions are called by the game engine when that object is first activated in the scene.

How are you currently creating these BuffObject instances?

If you want to utilise the standard MonoBehaviour functionality, it needs to be an active instance in the game scene. Try something like:

BuffObject my_buff_object = gameObject.AddComponent<BuffObject>();

(or the javascript equivalent)

You can’t create instances of classes that extend MonoBehaviour; you’ll get a warning if you try. Those are components that should be attached to GameObjects.

–Eric