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 /
  • Help Room /
avatar image
0
Question by Brisywill · Nov 29, 2017 at 08:39 PM · variablesscenesreferencing

Counting scenes

Hello all, Is there a way via code to check how many times a specific scene is loaded? I'd like to use the number of times one of my scenes is played in order to trigger an event. It seems like this was a bit easier to do in earlier releases in unity, but every method I try to use seems to yield nothing. This is what my code looks like:

 public class ToTheLivingRoom : MonoBehaviour {
     public bool thermoGet;
     public bool compGet;
     public bool bearGet;
     public Flowchart flowchart;
     public int loadTimes= 0;
     Scene scene = SceneManager.GetSceneByName("Office");
     public bool sceneActive; 
     
     // Use this for initialization
     void Start () {
         flowchart = GameObject.FindObjectOfType<Flowchart>();
         bearGet = flowchart.GetVariable("BearGet");
         thermoGet = flowchart.GetVariable("ThermoGet");
         compGet = flowchart.GetVariable("CompGet");
         
     }
 
     void Awake()
     {
         sceneActive = true;   
     }
     // Update is called once per frame
     void Update () {
         
         //if (scene.name("Office"))
         //{
         //    sceneActive == true;
         //}
         if (sceneActive == true)
         {
             loadTimes += 1;
 
             Debug.Log("The Office has loaded this many times:" + 1);
         }
 
         if (loadTimes == 3)
         {
             if (bearGet && thermoGet && compGet == true)
             {
                 flowchart.ExecuteBlock("A warning (Copy)");
             }
         }
         
         
     }
 }

As a note I am using fungus and referencing it in code, so I'm not sure if it's the two systems not playing nice or something I'm missing.

Thanks!

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 Hatsuko · Dec 01, 2017 at 10:05 PM

Hello. There are several ways to do it. But first thing to know is: what exactly is the problem. By default, when a scene is unloaded, everything in that scene is destroyed. For example, when you go from scene A to scene B, scene A is unloaded, and scene B is loaded. Everything in scene A is gone. Now if you go back to scene A, everything returns to its initial state, includes GameObject positions, variable values... etc.

 

So, if you store loadTimes in a usual way, by creating a field in a normal MonoBehaviour script, then this information is gone whenever scene changes. You need something that can persists between scenes so that you can store loadTimes without it returning to initial value whenever scene changes.

 

There are several ways to keep data between scenes. I haven't use Fungus before so I don't know if it has own feature that's specific to this problem? you might need to look for it yourself. But all you need to solve is this persistence problem. One way to solve it is using Static. You can try create a LoadTimesCounter script:

 public static class LoadTimesCounter {
 
     public static int officeLoadTimes;
 
 }

This script doesn't need to be attach to any GameObject. Then in your office script, do something like:

 using UnityEngine;
 
 public class Office : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         LoadTimesCounter.officeLoadTimes++;
         Debug.Log("Office has loaded " + LoadTimesCounter.officeLoadTimes + " times");
     }
 }
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

120 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

Related Questions

How to reference to a local variable from a struct 0 Answers

How to track variables by reference.. 0 Answers

Accessing a variable from another script (Bomberman Bomb Limit) 1 Answer

Access variables or methods from another class (or gameObject) [C#] 1 Answer

Find bool in multiple scenes? 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