- Home /
Why are the components on my script disappearing when I press play
When I press the play button the components I have become unlinked from the script they are supposed to be attached to (see the Create New Character Script)
Anyone know what's happening or how to fix it?
@death-be-upon-me, can you post your script so I can see what's going on? Also, are there any other scripts that are accessing your CreateNewCharacter script? $$anonymous$$aybe your BattleSystem or Player scripts?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CreateNewCharacter : $$anonymous$$onoBehaviour {
private BasePlayer newPlayer;
private string playerName;
private string playerGender;
private bool is$$anonymous$$elee = false;
private bool isSpeed = false;
private bool isRanged = false;
private bool isSecret = false;
private bool is$$anonymous$$ale = false;
public InputField getName;
public GameObject maleCharacterPic;
public GameObject femaleCharacterPic;
public Text toggleGenderText;
void Awake()
{
getName = GetComponent<InputField>();
maleCharacterPic = GetComponent<GameObject>();
femaleCharacterPic = GetComponent<GameObject>();
toggleGenderText = GetComponent<Text>();
}
void Update()
{
if (is$$anonymous$$ale == false)
{
femaleCharacterPic.SetActive(true);
maleCharacterPic.SetActive(false);
}
else
{
maleCharacterPic.SetActive(true);
femaleCharacterPic.SetActive(false);
}
}
public void ToggleGender()
{
if (is$$anonymous$$ale == true)
{
is$$anonymous$$ale = false;
toggleGenderText.text = "Female";
}
else if (is$$anonymous$$ale == false)
{
is$$anonymous$$ale = true;
toggleGenderText.text = "$$anonymous$$ale";
}
}
@Chasbas_80 I've changed the Awake() to Start() and tried putting the GetComponent parts in different voids but that didn't work
Are you initialising those variables in Start(), perhaps?
Answer by yogee · Jan 31, 2017 at 09:22 AM
try like this:
maleCharacterPic = transform.FindChild("CharacterPicMale").gameObject;
Thanks dude I've used this like you said for the make and female pics but I changed it a little for the other two.
void Start()
{
getName = transform.FindChild("InputField").GetComponent<InputField>();
toggleGenderText = transform.FindChild("ToggleGenderText").GetComponent<Text>();
maleCharacterPic = transform.FindChild("CharacterPic$$anonymous$$ale").gameObject;
femaleCharacterPic = transform.FindChild("CharacterPicFemale").gameObject;
}
Right, "GameObject" is not a component. So the line
GetComponent<GameObject>();
makes no sense. Why do you actually reassign those references manually via script? If you have assigned them in the editor you don't need all this. In fact if you want to assign them via script you should make your variables private as they don't need to show up in the inspector. This is bad design as fields exposed to the inspector are ment to be set inside the editor. However since you always overwrite the values in Start it's pointless to set them in the inspector.
@Bunny83 I made them private and it just gives me a NullReferenceException on all 4 of the variables
Your answer
Follow this Question
Related Questions
Enabling multiple Monobehaviour Components in a game object 1 Answer
Inheritance and Component Types in C# 2 Answers
Access water4 scripts via my script? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers