Update() not executed

I am new to Unity.
I added a script that works for new project. But it is executed only once when I attach it to an object under a folder for an existing project.

I looked document: Update is called if the MonoBehaviour is enabled. How can I enable MonoBehaviour?

Thanks!

Dave

function Update()
{
print(“Test1”);
// stop emitting particles
if (particleEmitter ) {
if (Rocket.PboatHits > 3)
{
particleEmitter.emit = true;
}
else
{
particleEmitter.emit = false;
}
}
else
{
particleEmitter.emit = false;
}

}

Every Component is already a MonoBehavior.

You may think about oriented object programmation.

To enable/disable a gameObject/component in the Inspector, just tick / untick the box near the name of the gameObject or the component. Enabling / Disabling a gameObject enable/disabled all of his component (but not his children nor his parents).

To enable/disable a gameObject during play, use gameObject.active = true (or false).

To enable/disable a component during play, use (usually) component.enabled = true (or false).
Exemple : GetComponent(PlayerScript).enabled = false;

Next :

  • By any chance, do you have any error message ?
  • What is Rocket ? PboatHits ?

It’s executing every frame, but you have “collapse” turned on in the console.

–Eric

Thank you both for your reply!

I took off the console collapse, I got many Update() executed. Thanks!

In my game, the player object should be on fire if it has been shot three times. So I need to detect the shoot times player object received at player side.
Rocket is the name of a script and PboatHits is a static variable.

However, at this time:
-program starts
-pirate shoots the first time, script at player side detected from static variable
-pirate shoots the 2nd, 3rd… times, Update() at player side is not executed and unable to detect number of shoots from the static variable

Any idea?

Thanks!

Dave

Test1
Detected pirate fires: 1
Test1
Detected pirate fires: 1
Test1
Detected pirate fires: 1
Test1
Detected pirate fires: 1
Test1
Detected pirate fires: 1

pirate fired =2
pirate fired =3
pirate fired =4
pirate fired =5

Add more Debug.Log everywhere to see where the values are going wrong, and show us the script you use to detect collisions and increment PboatHits.

Thanks! Here is my two scripts. I did not get any error when I run it. It seems that once the enemy object started to fire the 2nd, 3rd…, the detecting script is not executed, and the global variable PirateBall.pirateHits could not be accessed. I do not know where is my problem.

Script attached to player side to detect how many hits it has received:

function Update()
{
print(“Test1”);
if (particleEmitter ) {
print ("Detected pirate fires: " + PirateBall.pirateHits);
if (PirateBall.pirateHits > 3)
{
particleEmitter.emit = true;
}
else
{
particleEmitter.emit = false;
}
}
else
{
particleEmitter.emit = false;
}
}

The script attached to enemy object:

var explosionp2 : GameObject;
var timeOut = 5.0;
static var pirateHits = 0.0;

function Start () {
Invoke(“Kill”, timeOut);
}

function OnCollisionEnter (collision : Collision) {
var contact : ContactPoint = collision.contacts[0];
var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
Instantiate (explosionp2, contact.point, rotation);

// the fire hits are counted if it is collided with player object
pirateHits ++;
yield;
print(“pirate fired =” + pirateHits);
Kill ();
}

function Kill () {
var emitter : ParticleEmitter= GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = false;
transform.DetachChildren();
Destroy(gameObject);
}

@script RequireComponent (Rigidbody)

Thank you very much!

Few questions :

  • I assume the second script is named PirateBall ?
  • Why a yield ?
  • Why a transform.DetachChildren ?
  • Why are you looking for a ParticleEmitter in children ?

Little tweak :

  • var timeOut : float = 5.0;

That makes no difference; the code is exactly the same either way.

–Eric

I never said it would solve the problem but it is best to adopt good habits / manners earlier.

Thanks!

  • I assume the second script is named PirateBall ? Yes
  • Why a yield ? wait for one frame, something similar to: yield WaitForSeconds(5.0);- Why a transform.DetachChildren ? explorsion flame is a child of player object- Why are you looking for a ParticleEmitter in children ? same as above

The code works individually. But the first script (Update()) attached to player object is not executed after the enemy object fired the 2nd, 3rd … not sure the execution flow.

Dave

  • I meant : why do you need to wait one frame ?

  • I don’t think you need to detach something it if you’re going to destroy it immediatly, don’t you ?