- Home /
How do I fix a problem with disabling children in an empty object?
Hey guys, before I go into the question, I would just like to say I'm very new at programming so this may come across as a noob question. I'm creating a scene with a bunch of items that you can buy. I want to set up the scene so that everything starts off invisible, and when you buy the object, it will become visible and intractable. The code is as following:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;
using UnityEngine.Animations;
public class StockManager : MonoBehaviour
{
public int stock;
public GameObject Parent;
public string Name;
// Start is called before the first frame update
void Start()
{
Name = Parent.name;
Transform[] Children = Parent.GetComponentsInChildren<Transform>();
foreach (Transform child in Children)
{
child.gameObject.SetActive(false);
}
string Owning = PlayerPrefs.GetString(Name, Name);
if (Owning == Name)
{
foreach (Transform child in Children)
{
child.gameObject.SetActive(true);
}
}
}
So basically at the beginning of the script, it will de-activate all the children, and then when it grabs the PlayerPref that stores whether or not it's been purchased before, it will reactivate it. However, when I run the game, nothing happens and everything stays activated as it is in the editor. Does anyone know the problem or maybe some way to fix it?
I should probably say really quickly that there is nowhere in any of my code where I create any player prefs for these. So there is no way Unity can be thinking that the player has already purchased them.
Your answer
