• 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 Full-Metal Enriched Games · Mar 11, 2014 at 04:53 PM · c#instance

How to keep an instance through scenes

Hello everyone, we're working on our first Multiplayer Application using our Server Backend which is Planned for Open-Source release at a later date, however with using Unity3D we are having an issue with understanding how to keep the instance of the TcpClient connection alive through scenes in the Client. After some research we've discovered the method using (DontDestroyOnLoad) however this seems to not work how we want it to, as the Script starts becoming duplicated after loading the same scene multiple times. Basically, what we need to do is have a script that is running throughout the entire lifespan of the application which can be referenced from scripts at any moment. Writing the test-client for this was easy using C#~ However, now that we're working with Unity and it's transition of Scenes, how to go about properly doing this is completely boggling our developers minds. I guess that's one of the things you'll run into when the team isn't familiar with the tools provided.

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

Answer by TrevorP · Mar 11, 2014 at 05:00 PM

The way I plan to do this is by creating all of my persistent scripts in an "Entry" scene - that is never revisited after the game starts. This way I don't have to worry about duplicates being instantiated and I know all of my objects will follow the player from scene to scene.

Comment
Add comment · Show 2 · 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 Full-Metal Enriched Games · Mar 11, 2014 at 05:50 PM 0
Share

This is something we decided on doing, but how would you reference the scripts after you leave said scene. In order to do this would you have to make the Script not directly reference $$anonymous$$onoBehaviour?

avatar image TrevorP · Mar 11, 2014 at 07:15 PM 0
Share

In my situation I have an object called "Game$$anonymous$$anager" that doesn't get destroyed and is created only once. Any scripts that I want to keep around are kept on this Game$$anonymous$$anager object.

Through this method I know I can always reference the object Game$$anonymous$$anager to find the scripts I'm looking for - or if I want, I can use a "Game$$anonymous$$anager" script to act as a script manager. In your case, something like

 TcpClient tcpClient = GameObject.Find("Game$$anonymous$$anager").gameObject.getComponent<TcpClient>();


avatar image
1

Answer by whydoidoit · Mar 11, 2014 at 05:35 PM

Do what @TrevorP suggests or add an "ThereCanBeOnlyOne" script

ThereCanBeOnlyOne.cs

   using System.Collections;
   using UnityEngine;


    public class ThereCanBeOnlyOne : MonoBehaviour {
             public static ThereCanBeOnlyOne instance;

             void Awake() {
                 if(instance != null && instance != this) {
                     DestroyImmediate(gameObject);
                     return;
                 }
                 instance = this;
                 DontDestroyOnLoad(gameObject);
             }
    }
Comment
Add comment · Show 2 · 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 hamokshaelzaki · Dec 23, 2014 at 07:10 AM 1
Share

The problem with this script is that's it's usable only once in the project, if you added it to different objects, only one of them would be preserved

avatar image AngryCSharpNerd · Jun 28, 2015 at 06:48 PM 0
Share

I think adding Object.GetInstanceID() to the instance ID might work?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

C# QualitySettings.antiAliasing Black Screen 1 Answer

Jump Function C# hassle! 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