Instantiate() vs GameObject.Find()

excuse me i’m a little bit confused when using Instantiate() function…

so here’s my code

var playerMale : GameObject;
var playerFemale : GameObject;

var savedplayer : int = 0;

function Awake () {
		savedplayer = PlayerPrefs.GetInt("selectedplayer");
		
		playerMale = GameObject.Find("Cube1_M");
		playerFemale = GameObject.Find("testchar_F");

		if(savedplayer == 1){
			Instantiate(playerMale, Vector3(2,2,2), Quaternion.identity);
			playerFemale.SetActive(false);
			gameObject.AddComponent("PlayerStats");
			
		}
		else if(savedplayer == 2){
			Instantiate(playerFemale, Vector3(0,0,0), Quaternion.identity);
			playerMale.SetActive(false);
			//playerFemale.SetActive(true);
			gameObject.AddComponent("PlayerStats");
		}
}

that was how my character spawnned, but the problem is, the character selection in on different scene, so in THIS scene that contained script above there’s no object, so i confused,

to Instantiate i should decclare the playerMale as an GameObject, but GameObject.Find need the object itself… is there any way out from this?

Thanks before.

1 Answer

1

In my games I have the player as a prefab and just add it to the inspector and instantiate it that way.

yeah, i mean, when player choose character, we store the data right? it was the information that determine whether the player will be a, b, or c, how do i connect that to the instantiate that object? and also if i add it on inspector it wont show up on scene right?