- Home /
How do you properly work with GameObjects via scripts?
Hello there,
I've been starting doing smaller things with unity and I have the following problem:
I have a starting Scene where you can enter a name in a textfield. On clicking the StartButton:
It will run:
public class StartGame : MonoBehaviour {
public void startGame(Character myChar){
if (myChar != null) {
myChar.newCharacter ((GameObject.Find ("NameFieldText").GetComponent<Text> ()).text, int.Parse (GameObject.Find ("Strength").GetComponent<Text> ().text)
, int.Parse (GameObject.Find ("Dexterity").GetComponent<Text> ().text), int.Parse (GameObject.Find ("Intelligence").GetComponent<Text> ().text)
, int.Parse (GameObject.Find ("Wisdom").GetComponent<Text> ().text), int.Parse (GameObject.Find ("Luck").GetComponent<Text> ().text));
DontDestroyOnLoad(myChar.transform.root.gameObject);
Application.LoadLevel ("ScreenGame");
}
}
But now I have a problem with the way of GameObjects and Objects work. In the new scene I already have a CharacterNew GameObject.
GameObject assignChar = GameObject.Find ("CharacterNew");
GameObject loadedChar = GameObject.Find ("Character");
assignChar = loadedChar;
I thought I'd just assign the GO CharacterNew to the Character GO. With a Button I wanted to give the parameter to get the information from Character with CharacterNew.
My question is:
Does my GameObject "Character" still contain the information "myChar" in the new scene?
If yes, how can I get to those informations. If no, what would be a work around?
Thanks in advance.
Answer by Antrixullum · Dec 25, 2014 at 07:09 AM
Okay after a lot of trial and error I figured out that I was just dumb.
The Object has the information and with
Character loadedChar = GameObject.Find ("Character").GetComponent<Character> ();
Character assignChar = GameObject.Find ("CharacterNew").GetComponent<Character> ();
assignChar.loadedChar(getStats());
I could easily initilize the stats.
Your answer
