Hello, I am trying to make an object with many children disapear. This is my code in MaleNude.cs which is applied to the object.
using UnityEngine;
using System.Collections;
public class MaleNude : MonoBehaviour {
// Use this for initialization
void Start () {
if (renderer) renderer.enabled = false;
foreach (Transform child in transform){
// add the script and get a reference to it in mNude
MaleNude mNude = child.gameObject.AddComponent<MaleNude>();
mNude.ChangeEnabled(false); // use the mNude reference
}
}
// Update is called once per frame
void Update () {
}
public void ChangeEnabled(bool visible)
{
if (renderer) renderer.enabled = visible;
}
}
This code make my male character dissapear on first run which is great. But how do I turn him on from this script? I have added some code which should work but for some reason doesn’t.
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
//Variables
const int buttonWidth = 20;
const int typeWidth = 80;
string stringToEdit = "Max 20 characters";
int sex = 0;
int Shirt = 0;
int ShirtColor = 0;
int Pants = 0;
int PantsColor = 0;
int hair = 0;
int haircolor = 0;
int eyecolor = 0;
int Shoe = 0;
string debuglabel;
GameObject femaleNude;
FemaleNude femalenudeScript;
GameObject maleNude;
MaleNude malenudeScript;
// Use this for initialization
void Start () {
femaleNude = GameObject.Find("femalenude");
femalenudeScript = femaleNude.GetComponent<FemaleNude>();
femalenudeScript.ChangeEnabled(false);
maleNude = GameObject.Find("malenude");
malenudeScript = maleNude.GetComponent<MaleNude>();
}
// Update is called once per frame
void Update () {
}
//Menu Stuff
void OnGUI()
{
GUILayout.Label("Character Options:");
//Sex
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
sex -= 1;
if (sex == 0)
sex = 3;
changeSex(sex);
}
GUILayout.Box("Sex", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
sex += 1;
if (sex == 4)
sex = 1;
changeSex(sex);
}
GUILayout.EndHorizontal();
//HairStyles
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
hair -= 1;
if (hair == 0)
haircolor = 4;
changeHair(hair);
}
GUILayout.Box("Hair", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
hair += 1;
if (hair == 5)
hair = 1;
changeHair(hair);
}
GUILayout.EndHorizontal();
//HairColor
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
haircolor -= 1;
if (haircolor == 0)
haircolor = 4;
changeHairColor(haircolor);
}
GUILayout.Box("Hair Color", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
haircolor += 1;
if (haircolor == 5)
haircolor = 1;
changeHairColor(haircolor);
}
GUILayout.EndHorizontal();
//Eyes
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
eyecolor -= 1;
if (eyecolor == 0)
eyecolor = 4;
changeEyeColor(eyecolor);
}
GUILayout.Box("Eye Color", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
eyecolor += 1;
if (eyecolor == 5)
eyecolor = 1;
changeEyeColor(eyecolor);
}
GUILayout.EndHorizontal();
//Shirt
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
Shirt -= 1;
if (Shirt == 0)
Shirt = 5;
changeShirt(Shirt);
}
GUILayout.Box("Shirt", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
Shirt += 1;
if (Shirt == 6)
Shirt = 1;
changeShirt(Shirt);
}
GUILayout.EndHorizontal();
//ShirtColor
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
ShirtColor -= 1;
if (ShirtColor == 0)
ShirtColor = 9;
changeShirtColor(ShirtColor);
}
GUILayout.Box("Shirt Color", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
ShirtColor += 1;
if (ShirtColor == 10)
ShirtColor = 1;
changeShirtColor(ShirtColor);
}
GUILayout.EndHorizontal();
//Pants
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
Pants -= 1;
if (Pants == 0)
Pants = 3;
changePants(Pants);
}
GUILayout.Box("Pants", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
Pants += 1;
if (Pants == 4)
Pants = 1;
changePants(Pants);
}
GUILayout.EndHorizontal();
//PantsColor
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
PantsColor -= 1;
if (PantsColor == 0)
PantsColor = 9;
changePantsColor(PantsColor);
}
GUILayout.Box("Pants Color", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
PantsColor += 1;
if (PantsColor == 10)
PantsColor = 1;
changePantsColor(PantsColor);
}
GUILayout.EndHorizontal();
//Shoes
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(buttonWidth)))
{
Shoe -= 1;
if (Shoe == 0)
Shoe = 3;
changeShoe(Shoe);
}
GUILayout.Box("Shoes", GUILayout.Width(typeWidth));
if (GUILayout.Button(">", GUILayout.Width(buttonWidth)))
{
Shoe += 1;
if (Shoe == 4)
Shoe = 1;
changeShoe(Shoe);
}
GUILayout.EndHorizontal();
//Name
GUILayout.BeginHorizontal();
GUILayout.Label("Name:");
stringToEdit = GUILayout.TextField(stringToEdit,20);
GUILayout.Button("Change", GUILayout.Width(typeWidth));
GUILayout.EndHorizontal();
//Load
GUILayout.BeginHorizontal();
if (GUILayout.Button("Load", GUILayout.Width(typeWidth)))
{
debuglabel = PlayerPrefs.GetString("testpref");
}
GUILayout.EndHorizontal();
//Save
GUILayout.BeginHorizontal();
if (GUILayout.Button("Save", GUILayout.Width(typeWidth)))
{
PlayerPrefs.SetString("testpref", "testvalue");
}
GUILayout.EndHorizontal();
//Delete
GUILayout.BeginHorizontal();
if (GUILayout.Button("Delete Saved File", GUILayout.Width(160)))
{
PlayerPrefs.DeleteKey("testpref");
debuglabel = "";
}
GUILayout.EndHorizontal();
//Exit
GUILayout.BeginHorizontal();
if (GUILayout.Button("Quit", GUILayout.Width(typeWidth)))
Application.LoadLevel("MainMenu");
GUILayout.EndHorizontal();
//Debugging
GUILayout.BeginHorizontal();
GUILayout.Label(debuglabel);
GUILayout.EndHorizontal();
}
void changeSex(int sex)
{
if (sex == 1)
{
femalenudeScript.ChangeEnabled(false);
malenudeScript.ChangeEnabled(true);
}
if (sex == 2)
{
femalenudeScript.ChangeEnabled(true);
malenudeScript.ChangeEnabled(false);
}
if (sex == 3)
{
femalenudeScript.ChangeEnabled(false);
malenudeScript.ChangeEnabled(false);
}
}
void changeHair(int hair)
{
if (sex == 1)
{
if (hair == 1)
debuglabel = "Male Hairstyle 1";
if (hair== 2)
debuglabel = "Male Hairstyle 2";
if (hair == 3)
debuglabel = "Male Hairstyle 3";
if (hair == 4)
debuglabel = "Male Hairstyle 4";
}
if (sex == 2)
{
if (hair == 1)
debuglabel = "Female Hairstyle 1";
if (hair== 2)
debuglabel = "Female Hairstyle 2";
if (hair == 3)
debuglabel = "Female Hairstyle 3";
if (hair == 4)
debuglabel = "Female Hairstyle 4";
}
}
void changeHairColor(int haircolor)
{
if (haircolor == 1)
debuglabel = "Haircolor 1";
if (haircolor == 2)
debuglabel = "Haircolor 2";
if (haircolor == 3)
debuglabel = "Haircolor 3";
if (haircolor == 4)
debuglabel = "Haircolor 4";
}
void changeEyeColor(int eyecolor)
{
if (eyecolor == 1)
debuglabel = "Eye color 1";
if (eyecolor == 2)
debuglabel = "Eye color 2";
if (eyecolor == 3)
debuglabel = "Eye color 3";
if (eyecolor == 4)
debuglabel = "Eye color 4";
}
void changeShirt(int Shirt)
{
if (sex == 1)
{
if (Shirt == 1)
debuglabel = "Male Shirt 1";
if (Shirt == 2)
debuglabel = "Male Shirt 2";
if (Shirt == 3)
debuglabel = "Male Shirt 3";
if (Shirt == 4)
debuglabel = "Male Shirt 4";
if (Shirt == 5)
debuglabel = "Male Shirt 5";
}
if (sex == 2)
{
if (Shirt == 1)
debuglabel = "Female Shirt 1";
if (Shirt == 2)
debuglabel = "Female Shirt 2";
if (Shirt == 3)
debuglabel = "Female Shirt 3";
if (Shirt == 4)
debuglabel = "Female Shirt 4";
if (Shirt == 5)
debuglabel = "Female Shirt 5";
}
}
void changeShirtColor(int ShirtColor)
{
if (sex == 1)
{
if (ShirtColor == 1)
debuglabel = "Male Shirt Color 1";
if (ShirtColor == 2)
debuglabel = "Male Shirt Color 2";
if (ShirtColor == 3)
debuglabel = "Male Shirt Color 3";
if (ShirtColor == 4)
debuglabel = "Male Shirt Color 4";
if (ShirtColor == 5)
debuglabel = "Male Shirt Color 5";
if (ShirtColor == 6)
debuglabel = "Male Shirt Color 6";
if (ShirtColor == 7)
debuglabel = "Male Shirt Color 7";
if (ShirtColor == 8)
debuglabel = "Male Shirt Color 8";
if (ShirtColor == 9)
debuglabel = "Male Shirt Color 9";
}
if (sex == 2)
{
if (ShirtColor == 1)
debuglabel = "Female Shirt Color 1";
if (ShirtColor == 2)
debuglabel = "Female Shirt Color 2";
if (ShirtColor == 3)
debuglabel = "Female Shirt Color 3";
if (ShirtColor == 4)
debuglabel = "Female Shirt Color 4";
if (ShirtColor == 5)
debuglabel = "Female Shirt Color 5";
if (ShirtColor == 6)
debuglabel = "Female Shirt Color 6";
if (ShirtColor == 7)
debuglabel = "Female Shirt Color 7";
if (ShirtColor == 8)
debuglabel = "Female Shirt Color 8";
if (ShirtColor == 9)
debuglabel = "Female Shirt Color 9";
}
}
void changePants(int Pants)
{
if (sex == 1)
{
if (Pants == 1)
debuglabel = "Male Pants 1";
if (Pants == 2)
debuglabel = "Male Pants 2";
if (Pants == 3)
debuglabel = "Male Pants 3";
}
if (sex == 2)
{
if (Pants == 1)
debuglabel = "Female Pants 1";
if (Pants == 2)
debuglabel = "Female Pants 2";
if (Pants == 3)
debuglabel = "Female Pants 3";
}
}
void changePantsColor(int PantsColor)
{
if (sex == 1)
{
if (PantsColor == 1)
debuglabel = "Male Pants Color 1";
if (PantsColor == 2)
debuglabel = "Male Pants Color 2";
if (PantsColor == 3)
debuglabel = "Male Pants Color 3";
if (PantsColor == 4)
debuglabel = "Male Pants Color 4";
if (PantsColor == 5)
debuglabel = "Male Pants Color 5";
if (PantsColor == 6)
debuglabel = "Male Pants Color 6";
if (PantsColor == 7)
debuglabel = "Male Pants Color 7";
if (PantsColor == 8)
debuglabel = "Male Pants Color 8";
if (PantsColor == 9)
debuglabel = "Male Pants Color 9";
}
if (sex == 2)
{
if (PantsColor == 1)
debuglabel = "Female Pants Color 1";
if (PantsColor == 2)
debuglabel = "Female Pants Color 2";
if (PantsColor == 3)
debuglabel = "Female Pants Color 3";
if (PantsColor == 4)
debuglabel = "Female Pants Color 4";
if (PantsColor == 5)
debuglabel = "Female Pants Color 5";
if (PantsColor == 6)
debuglabel = "Female Pants Color 6";
if (PantsColor == 7)
debuglabel = "Female Pants Color 7";
if (PantsColor == 8)
debuglabel = "Female Pants Color 8";
if (PantsColor == 9)
debuglabel = "Female Pants Color 9";
}
}
void changeShoe(int Shoe)
{
if (sex == 1)
{
if (Shoe == 1)
debuglabel = "Male Shoe 1";
if (Shoe== 2)
debuglabel = "Male Shoe 2";
if (Shoe == 3)
debuglabel = "Male Shoe 3";
if (Shoe == 4)
debuglabel = "Male Shoe 4";
}
if (sex == 2)
{
if (Shoe == 1)
debuglabel = "Female Shoe 1";
if (Shoe== 2)
debuglabel = "Female Shoe 2";
if (Shoe == 3)
debuglabel = "Female Shoe 3";
if (Shoe == 4)
debuglabel = "Female Shoe 4";
}
}
}