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 MysticalSkyWhale · Nov 17, 2020 at 10:05 PM · gameobjectscenebeginnerscene-switching

Affecting Game Objects in another Scene

Hello there,

I'm currently working on a project that has an overworld in which the player spends most of their time, but when they collide with a wandering enemy, they get loaded into a different scene where they resolve the fight using turn-based combat. The problem I'm having is that when the combat finishes, the enemy's overworld GameObject should be destroyed so that the player cannot collide with it again.

The code I have to load a player into the battle scene is this:

 public class EnemyWorldBehavior : MonoBehaviour
 {
     public string battleScene;
     
     void OnTriggerEnter(Collider collider)
     {
         if(collider.gameObject.tag == "Player" && battleScene != null)
         {
             SceneManager.LoadScene(battleScene, LoadSceneMode.Single);
             // This is where I'd destroy the Enemy GameObject
         }
     }
 
     //TODO: Add AI that makes the enemy wander throughout the scene somewhat
 }

And the code I have that ends the battle is this: (Note: I followed Brackey's Tutorial on turn-based combat, so the rest of the code is almost a carbon copy of that. Some changes were made to better suit my project and fix a glaring issue in his tutorial)

 IEnumerator EndBattle()
     {
         if(state == BattleState.WIN)
         {
             dialogueText.text = "That's a win for you!";
             yield return new WaitForSeconds(2);
             SceneManager.LoadScene("SampleScene", LoadSceneMode.Single);
         }
         else if(state == BattleState.LOSE)
         {
             dialogueText.text = "You have died...";
         }
     }

I have it set up so that every enemy prefab can go to their own battle scene (which is my current workaround for making each battle pseudorandom with different numbers of enemies appearing in each battle). How can I set it up so that once the battle is resolved (and the player wins) the enemy GameObject in the overworld either calls a function to drop loot and die or is just simply destroyed?

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
1
Best Answer

Answer by rh_galaxy · Nov 18, 2020 at 04:45 AM

You need to have some code that manages the scene switching, that does not itself get swapped along with the scene change. A GameManager.

This is an attempt at how this can be done. But it's not tested and not finished, but you can get an idea.

 public class GameManager : MonoBehaviour
 {
     //implemented as singleton do not destroy
 
     int state = 0;
     void Update()
     {
         switch(state)
         {
             case 0:
                 SceneManager.LoadScene("SampleScene", LoadSceneMode.Single);
                 state++;
                 break;
             case 1: //in overworld
                 if(EnemyWorldBehavior.theOverWorld.enterBattle)
                 {
                     SceneManager.LoadScene("BattleScene", LoadSceneMode.Single);
                     state++;
                 }
                 break;
             case 2: //in battle scene
                 if(BattleScene.theBattle.gameOver)
                 {
                     SceneManager.LoadScene("SampleScene", LoadSceneMode.Single);
                     EnemyWorldBehavior.theOverWorld.Init();
                     state = 1;
                 }
                 break;
         }
     }
 }
 
 public class EnemyWorldBehavior : MonoBehaviour
 {
     internal static EnemyWorldBehavior theOverWorld = null;
     internal bool enterBattle = false;
     
     private void Awake()
     {
         theOverWorld = this;
     }
     public void Init(/*how to init*/)
     {
         //delete the enemy here
         //...
     }
     void OnTriggerEnter(Collider collider)
     {
         if(collider.gameObject.tag == "Player")
         {
             enterBattle = true;
         }
     }
 }
 public class BattleScene : MonoBehaviour
 {
     internal static BattleScene theBattle = null;
     internal bool gameOver = false;
 
     private void Awake()
     {
         theBattle = this;
     }
     void Update()
     {
         //set gameOver=true when win or lose
         //the scene will change when set
         //...
     }
 }
Comment
Add comment · Show 1 · 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 MysticalSkyWhale · Nov 18, 2020 at 07:15 AM 0
Share

This seems to be the way to go. I've looked up some tutorials on the subject for more ideas on how to implement it. It'll be a long and buggy process, but thanks for pointing me in the right direction.

avatar image
0

Answer by Hexes22 · Nov 18, 2020 at 02:47 AM

after being put into the overworld again and winning write some code that destroys the enemy

Comment
Add comment · Show 1 · 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 MysticalSkyWhale · Nov 18, 2020 at 02:56 AM 0
Share

I've tried that, but I don't know how to access the proper variables in my BattleState enum. Plus the code that ends the battle simply reloads the old scene entirely which respawns the enemies.

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

254 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

Related Questions

Why is my gameobject icon doesn't appear in the scene 1 Answer

Serialization of gameobjects. JSON, XML, Binary. 1 Answer

How can I check the scene and play audio accordingly? 1 Answer

Game Development Approach 0 Answers

,Enums not being passed between scripts properly 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