- Home /
Don't render GameObject on load if already hit by player?
I hope I can make proper sense of my issue. I'm on a small platformer project for practice. I have boxes the player can break. Once you break a box, the material is changed so it looks "used". I can't use static variables because there is more than one instance (multiple boxes with same script attached.). I have some bonus rounds the player can enter (new scene). When the player is done w/ bonus, he goes back to the original scene file using Application.LoadLevel(last level I was in). The boxes are back to normal, and I can break them again and get higher and higher score. I don't want this. I want the boxes to still be "used" when I go back to the level. I have no idea where to look but here, I can't even think of what I would google for this...Note: DontDestroyOnLoad doesn't work either, because it just doubles the objects. The new ones are loaded, the old ones are still there, so there ends up being two boxes in one's place.
Why don't you create a static manager for this ins$$anonymous$$d? Surely you can't apply static variables to the boxes because there will be multiple instances of it, but what about a Box$$anonymous$$anager? A static manager that maintains each state of the box?
Answer by clunk47 · Nov 02, 2013 at 11:04 PM
One way you could do this would be to write a script with a static generic list of names. Just name the boxes accordingly (box1, box2, box3, etc..) in your hierarchy. Once you hit the box, have a script in your box add the box name to the static list. Say you have a script named BoxNames.cs:
//BoxNames.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BoxNames : MonoBehaviour
{
public static List<string> names = new List<string>();
}
Then say you have a script on your box with a variable named 'isEmpty', which is a boolean. Let 'isEmpty' determine what materials to use on the box, pretty much which "state" the box is in. When you hit the box, make isEmpty true, and add your box name to the list. Then when you reload the scene, check if your current box's name exists in the static list. If it does, make empty true on Start().
Box.cs
using UnityEngine;
using System.Collections;
public class Box: MonoBehaviour
{
bool isEmpty;
void Start()
{
if(BoxNames.names.Contains(gameObject.name))
isEmpty = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613121133im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why should I use a game object for non-physical things? 2 Answers
Static List< GameObject> in Singleton 1 Answer
Using a static class & pass in gameObject or just add same script to multiple objects 1 Answer
What do the "active" and "static" checkboxes do? 1 Answer
Attaching GameObject and ParticleSystem to C# script 1 Answer