Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 tadpolili · Jun 19, 2017 at 01:13 PM · c#unity 2dcollider2dstate-machinecutscene

DIsabling a Box Collider Trigger after cut scene

So I currently have a 2D Box Collider with the is trigger option checked. Once the player enters that area a new scene (the battle scene where battling takes place) is loaded. Once the battle scene ends and I load the previous scene, since the player is still on the spot of the trigger it activates again, loading the battle scene again.

This creates a continuous loop. I'm not sure where to put the code to disable this or if there is a better method to handling location triggered cutscenes/fight scenes.

I have a global game state script that handles the states of the game. Here is an example of my state machine code:

     void Awake(){
 
         DontDestroyOnLoad (transform.gameObject);
         if (instance == null) {
             instance = this as GameStateMachine;
         } else {
             Destroy (gameObject);
         }
         gameState = GameState.PROCESSING;
     }
     // Update is called once per frame
     void Update () {
 
         switch (gameState) {
         case(GameState.WORLD):
             //Didint Work
             collider = GameObject.Find ("StartBattle");
             Debug.Log ("collider" + collider);
             if (zero == 1) {
                 if (collider) {
                     Debug.Log ("turning off collider");
                     collider.SetActive (false);
                 }
             }
             //Didnt Work ^^
             SceneManager.LoadScene("main");
 
             gameState = GameState.PROCESSING;
             break;
         case(GameState.TITLESCREEN):
             gameState = GameState.PROCESSING;
             break;
         case(GameState.BATTLE):
             Debug.Log ("Is working");
             gameState = GameState.PROCESSING;
             zero++;
             SceneManager.LoadScene("Battle");
             break;
         case(GameState.PROCESSING):
             break;
         }
     }




Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheDJBuntin · Jul 03, 2017 at 08:09 PM

Ok, so the simple answer to your question is to put this whereever you want the it to stop being the trigger:

 GetComponent</*WhateverColliderYouAreUsing*/>().isTrigger = false;

Something I noticed in your code which you should learn and fix immediately is: You do "GameObject.Find(...)" in Update(), this is very very bad because the game will be searching through all game objects for one with this name every single frame Have this set in Start() instead so it only needs to be found once.

Comment
Add comment · Share
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
0

Answer by Raimi · Jul 04, 2017 at 12:43 AM

Your state machine doesn't look well formed. For one you shouldn't have it all in update. You should use events and delegates. This way you run state specific code when an event is triggered, like on state change, rather than checking state every frame.

 public enum States {SceneEnter, SceneExit, ScenePause, ScenePlay, GameOver};
 public States state;
 public States lastState;
 
 public delegate void OnStateChange ();
     public static event OnStateChange EnterScene;
     public static event OnStateChange ExitScene;
     public static event OnStateChange PlayScene;
     public static event OnStateChange PauseScene;
     public static event OnStateChange GameOver;
     
     public void SetState(States newState)
     {
         lastState = state;
         state = newState;
         
         switch (state) 
         {
              case States.SceneEnter:
         
                 EnterScene ();
         
              break;
         
              case States.SceneExit:
         
                 ExitScene ();
         
              break;
         
              case States.ScenePlay:
         
                 PlayScene ();
         
              break;
         
              case States.ScenePause:
         
                 PauseScene ();
         
              break;
         
              case States.GameOver:
         
                 GameOver ();
         
              break;
         }
 }

Your other scripts can subscribe to events and only run code when a specific event has occurred on state change. When you call SetState in this example it will store the last state, change the state to new state and fire an event based on the state. Your code thats subscribed to the event will be triggered and run code specific to that event. Only subscribed scripts will be trigger by that event.

Sorry this is a bit convoluted, but state machines can be very complex and powerful and hard to explain in a short answer.

There is lots of info on events a delegates out there, please research them, they are very powerful. Again Im very sorry I cant explain better, I want to help but finding it hard to explain in a straight forward way.

Comment
Add comment · Share
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

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

330 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Block Game Object interaction with another Game Object with kinematic active 0 Answers

How to make my object bounce off the screen bounds? 1 Answer

Problem with 2D Collider Trigger 1 Answer


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