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 Ochreous · Nov 22, 2013 at 09:26 PM · c#classdontdestroyonload

C# Unity 3d Don't Destroy Class On Load

I want to preserve a specific class on a gameobject and non of the other scripts attached to that gameobject. Is there a way to specify that?

Comment
Add comment · Show 1
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 Tomer-Barkan · Nov 23, 2013 at 06:02 AM 0
Share

I don't think there is a way, and even if there is, I'm not sure how exactly it would work... How would you access this script if the rest of the GameObject would cease to exist?

$$anonymous$$aybe you better explain what your end goal is and the community might help you find alternatives.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by clunk47 · Nov 22, 2013 at 10:24 PM

 void Start()
 {
     DontDestroyOnLoad(this);
 }


DontDestroyOnLoad Reference

Comment
Add comment · Show 3 · 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 Tomer-Barkan · Nov 23, 2013 at 06:00 AM 0
Share

He specifically said that he wanted to save only one of the scripts attached to the game object. According to your link:

If the object is a component or game object then its entire transform hierarchy will not be destroyed either.

So the answer is incorrect.

avatar image clunk47 · Nov 23, 2013 at 09:41 PM 0
Share

You mean he wants to destroy all scripts except for one?

 foreach(Component com in GetComponents<Component>())
 {
     if(com != this && com != GetComponent<Transform>())
         Destroy (com);
 }

He never said he wanted to destroy the gameObject or its children, just all scripts but one attached to the gameObject. In order to use any $$anonymous$$onoBehaviour at runtime, it needs to be attached to a gameObject, otherwise you're talking static classes that derive from System.Object...

avatar image Tomer-Barkan · Nov 24, 2013 at 08:31 AM 0
Share

Yeah, I'm not sure exactly what he meant, but he did say "preserve a specific class and non of the other scripts". $$anonymous$$aybe he meant one object and none of the other objects... Well, anyway he accepted this answer so it must have helped him.

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

17 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

Related Questions

C# Accessing Variables from Class Variable 1 Answer

How to execute a Skript global? 2 Answers

Player lives script help 1 Answer

C# -- Build character unit from script 1 Answer

Conversion of a static class to struct? 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