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 Brijac · Jan 29, 2021 at 04:13 PM · playerprefscollider2dtriggersif statement

How to make a script work in scene1 only if a collider in scene2 is triggered

So, I'm making this game that has a Level Select screen with few levels and then each of those levels has a scene. This is the LevelSelect scene: alt text And this is the script attached to the LevelManager in this scene:

     private void Start()
     {
         PlayerPrefs.SetInt("Scene1stars", 3);
 
         for (int i = 0; i < levelLoaders.Length; i++)
         {
             if(i == 0)
             {
                 if(PlayerPrefs.GetInt("Scene1stars") == 0)
                 {
                     levelLoaders[i].interactable = false;
                 }
                 for (int j = 0; j < starsOne.Length; j++)
                 {
                     if (PlayerPrefs.GetInt("Scene2stars") > j)
                     {
                         starsOne[j].gameObject.SetActive(true);
                     }
                 }
             }
             if (i == 1)
             {
                 if (PlayerPrefs.GetInt("Scene2stars") == 0)
                 {
                     levelLoaders[i].interactable = false;
                 }
                 for (int j = 0; j < starsTwo.Length; j++)
                 {
                     if (PlayerPrefs.GetInt("Scene3stars") > j)
                     {
                         starsTwo[j].gameObject.SetActive(true);
                     }
                 }
             }
             if (i == 2)
             {
                 if (PlayerPrefs.GetInt("Scene3stars") == 0)
                 {
                     levelLoaders[i].interactable = false;
                     for (int j = 0; j < starsThree.Length; j++)
                     {
                         if (PlayerPrefs.GetInt("Scene4stars") > j)
                         {
                             starsThree[j].gameObject.SetActive(true);
                         }
                     }
                 }
             }
             if (i == 3)
             {
                 if (PlayerPrefs.GetInt("Scene4stars") == 0)
                 {
                     levelLoaders[i].interactable = false;
                 }
             }
         }
     }

This is a scene of the level1 for example (three stars on level 1 are just example of what happens if all three points in level 1 are collected): alt text It has this script attached to the "points":

 public class PickUpPoints : MonoBehaviour
 {
     public string levelName;
 
     private void Start()
     {
         levelName = SceneManager.GetActiveScene().name;
     }
 
     private void OnTriggerEnter(Collider other)
     {
         PlayerPrefs.SetInt("Scene" + (SceneManager.GetActiveScene().buildIndex + 1 )+ "stars", PlayerPrefs.GetInt("Scene" + (SceneManager.GetActiveScene().buildIndex + 1) + "stars") + 1);
     }
 }

Now, the problem is, if I for example click on level1 in the Level Selection screen to start the level1 scene and then while playing collect two points I will get two stars in the Level Selection scene even if I quit the "play" in the editor or touch a game over colliders (up or bottom black lines). I would like to make those stars appear in the Level Selection scene only if the player touches the Level finish collider, otherwise, I would like those points and stars not to be saved in the playerprefs. How can I do that?

levelselect.png (19.9 kB)
levelscene.png (4.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Jan 30, 2021 at 12:26 PM

You could use a static script and a start script to keep track of bools in different scenes. For example:

 public static class ControlBools
 {
       public bool ColliderWasTriggered;
 }

Then, when you enter the collider in Scene2, you can set the bool to true. For example:

 void OnCollisionEnter(Collider collision)
 {
      // do collision stuff
 
      ControlBools.ColliderWasTriggered = true;
 }

Finally, whenever you load Scene1, there needs to be a script that has a reference to the script you want to enable/disable, depending on if the collider was entered in scene2. For example:

 public class StartScript : MonoBehaviour
 {
      public Scene1Script script;
 
      void Start()
      {
           if (ControlBools.ColliderWasTriggered)
                script.enabled = false;
      }
 }
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

118 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

Related Questions

Trigger Triggers Without Collision 1 Answer

How to use OnTriggerEnter on different gameobject to detect collision? 3 Answers

Comparing the value of a peg to its neighboring pegs' values 0 Answers

(C#) If statements based on variables in another script 1 Answer

Multiple Hitbox child Objects (King of Fighters) 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