- Home /
Script Not Attached to Scene Still Triggers Events
I'm still a bit confused about when and if a script actually has to be attached to an object in a scene in order to run. If anyone can explain that to me off the bat, I'd really appreciate it.
My more specific issue is that I have a script, which is not attached to anything in my scene, but still appears to be running and sending events. Here is the code:
using UnityEngine;
using System.Collections;
public class SetMessagesScene1 : MonoBehaviour {
/*************************************
* This class contains messages specific to Scene1.
*
* ***********************************/
void OnEnable () {
EventManager.checkpointReached += HandleEventManagercheckpointReached;
}
void OnDisable(){
EventManager.checkpointReached -= HandleEventManagercheckpointReached;
}
void HandleEventManagercheckpointReached (int id)
{
if(id==0){
EventManager.messageSend("WARP BEACON REACHED. TRANSPORTS MUST WAIT UNTIL THEY LOCATE THE NEXT BEACON.");
}
if(id==2){
EventManager.messageSend("TARGET THE TURRETS! LOOK OUT FOR MISSLES!");
}
}
}
This script is set up to listen for the "checkpointReached" event, then send a message for certain checkpoints. I attached this script to an object in Scene1 and it worked as expected.
In Scene2, I didn't attach it to anything at all, but it still sends messages whenever checkpoints are reached. In Scene2 I want to send out a different set of messages, so I'm a little trouble that this script from Scene1 still seems to be running. Can anyone tell me why that is?
For your first questions.
Scripts have to be attached to objects to run when they inherit from $$anonymous$$onobehaviour. If the script doesn't do this, it doesn't have to be attached to an object. But it will have to have an instance to make calls to.
Well that is confusing then, because the above script is not attached to an object and yet it is still running. I have a couple other scripts that inherit from $$anonymous$$onobehaviour (an Event$$anonymous$$anager and a Scene$$anonymous$$anager) that are not attached to objects either, but they are running and they control the entire game.
Your answer
