Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 toborific · Jan 13, 2014 at 12:29 AM · listenerevent-handling

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?

Comment
Add comment · Show 2
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 elforama · Jan 13, 2014 at 02:29 AM 0
Share

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.

avatar image toborific · Jan 13, 2014 at 03:24 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

listen for flash events 2 Answers

Why is checking button click in Unity done this way? 1 Answer

OnPointerDown vs OnMouseDown 1 Answer

Why does my code hang when calling a script in a multi-nested event function 0 Answers

Having a problem with Audio Reverb Zones. 0 Answers


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