- Home /
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?
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.
Answer by clunk47 · Nov 22, 2013 at 10:24 PM
void Start()
{
DontDestroyOnLoad(this);
}
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.
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...
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
Follow this Question
Related Questions
C# Accessing Variables from Class Variable 1 Answer
How to execute a Skript global? 2 Answers
Player lives script help 1 Answer