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 Pokrzywnik · Jul 02, 2017 at 01:09 AM · scripting problemgamescene-change

How to create script linked to scene?

Hi, i create a script "finish.cs", it have 2 public values:

-maxScore

-next_level

that are changeable from inspecor. I want to make this script universal for each scene, but when i edit eg. maxScore in scene 1, this value is assigned aslo to maxScore from scene 2. How can i avoid this? here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 
 public class finish : MonoBehaviour {
 
     public TextMesh textMesh;   //connect score display text
     public int maxScore = 3;    //max score on level
     public string next_level;   //next level name
 
     void Start()
     {
         
     }
 
     private void OnTriggerEnter(Collider collision)
     {
         if (collision.gameObject.name == "Sphere")
         {
             string wynik = textMesh.text;       //conversion of score
             int Iwynik;                         //to int and check if
             int.TryParse(wynik, out Iwynik);    //its equal to max score
 
             if (Iwynik == maxScore)
             {
                 Debug.Log("level change");      //just debug
                 textMesh.text = "0";                    //score reset
                 SceneManager.LoadScene(next_level);     //load next scene
             }
 
 
             
         }
         Debug.Log(collision.gameObject.name);   //just debug
 
     }
 }
 

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

Answer by Mercbaker · Jul 02, 2017 at 01:39 AM

When you hit run:

  • finish.class is created and maxScore is assigned to 3

  • When you switch scenes, finish.class is Destroyed

  • When you load another scene that requres finish.class, it is created again and maxScore is assigned to 3 (once again)

If you want data to persist between scene changes you need to make those classes a "Singleton".

A very convenient way to achieve this is to make a class with the following code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Singleton<T> : MonoBehaviour where T : MonoBehaviour {
 
     private static T instance;
 
     public static T Instance {
         get {
             T obj = FindObjectOfType<T>();
 
             if (instance == null) {
                 instance = obj;
             } else if (instance != obj) {
                 Destroy(obj);
             }
             obj.transform.SetParent(null);
             DontDestroyOnLoad(obj);
             return instance;
         }
     }
 }

Now instead of finish.class extending MonoBehavior, make it extend the Singleton.

 public class finish : Singleton<finish>{

Your variable will not be destroyed and replaced when you switch scenes. Remember to only make classes a "Singleton" if they are unique.

Good singleton classes are GameManagers, PlayerInventory, SceneManagers, or stuff like Appendixes. All stuff that should be static.

Comment
Add comment · Show 6 · 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 Pokrzywnik · Jul 02, 2017 at 01:56 AM 0
Share

Sorry i see small fail in my code, there shoudn't be "3" just empty value, to choose from inspector. So i made a new script named Singleto, and then in my finish extend it with this script? Sorry for stupid question but im not england. And after doing so i can easily manage those 2 values for every scene independent?

avatar image Mercbaker Pokrzywnik · Jul 02, 2017 at 02:02 AM 0
Share

If you implement the above you can switch scenes and keep the original values on the script.

avatar image Pokrzywnik · Jul 02, 2017 at 02:01 AM 0
Share

Works like a charm :D thank you so much :)

avatar image Bampf · Jul 02, 2017 at 03:02 AM 1
Share

This a time-honored solution, explained very well here. I'll just add a couple comments.

  1. All code that needs to reference the singleton must use the static method Instance. In this case, the finish class singleton would be found by calling finish.Instance. (I recent had code that was calling FindObjectOfType to find a singleton object. This almost always worked, but there was a subtle bug when it is called in the first frame of a new scene-- it can find the duplicate object before the singleton code (line 16) has finished destroying it.)

  2. The singleton's Instance method calls FindObjectByType every time, and that's a slow function. So if you have a GameObject calling Singleton.Instance inside its Update loop, you should ins$$anonymous$$d do it once and store the result in a local variable.

avatar image Pokrzywnik Bampf · Jul 02, 2017 at 06:46 PM 0
Share

Thanks for extra comments, in my code finish execute only once per scene - on collision, also changing scenes always take some time, even in main menu you need about 5seconds before choosing first level. So i think its enough time to create new instance and delete old, for now it works really well. But for future can you give me example how i can easy save instance in variable? ( if i need someday use it with update loop script) I hope that i wont use singleton again, dont want to overuse it but if someday i need singelot for self updating script i wont have any idea how to implement it. Its A new thing for me, thanks again for your reply :)

avatar image Bunny83 · Jul 02, 2017 at 07:37 PM 3
Share

Your singleton getter implementation shouldn't call FindObjectOfType every time. If you do that you wouldn't need any static variable at all. The point of the static variable is to provide a quick access to the "one" instance.

It should be something like this:

 private static T instance = null;
 public static T Instance {
     get {
         if (instance == null) {
             instance = FindObjectOfType<T>();
             if (instance == null)
                 instance = (new GameObject(typeof(T).Name)).AddComponent<T>();
             instance.transform.SetParent(null);
             DontDestroyOnLoad(instance);
         }
         return instance;
     }
 }

Implementing a check for destroying duplicates inside the getter makes no sense. "FindObjectOfType" simple returns "the first" instance it encounters. So it can return the same instance that is stored inside the static variable but there could still be another instance which you wouldn't catch that way.

A common way is to implement the destroy check in Awake:

 protected virtual void Awake()
 {
     if (this != Instance)
         Destroy(gameObject);
 }
avatar image
0

Answer by ahmad-hadia · Jul 02, 2017 at 01:47 AM

okay first you must to build scenes and you know, then you can declare script in other scenes ,or another solve to create game object in scene 1 and add script to it ,and use (dont destroy on load) .

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

133 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

Related Questions

SceneManager.LoadSceneAsync stops coroutine 3 Answers

Why wont my character stop moving when it collides with the obstacles?,Why doesn't my movement script stop? 0 Answers

Out of memory during play 0 Answers

How can I un-invert my first person spaceship controls in C#? 1 Answer

Programming the Judgement Wheel (Shadow Hearts) 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