- Home /
Enter new level positions - check for players that might not be there
Hello unites! I travel with a party in my rpg- at times they may not be there (I recall some/ they may leave). Some of them have the same tags. How do I set it up to check if they are / are not null when I load a new level?
I have tried this in the start function for a spawn point
Pet = GameObject.FindWithTag("Pet").transform; Pet2=GameObject.FindWithTag("Pet2").transform; //etc finds...
Player = GameObject.FindWithTag("Player").transform; cam = GameObject.FindWithTag("MainCamera").transform;
Player.position = transform.position; Player.rotation.y = transform.rotation.y; cam.rotation.y = transform.rotation.y;
if(Pet!=null){ Pet.position =transform.position+transform.right*4; Pet.rotation.y = transform.rotation.y;
}if(Pet2 != null){ Pet2.position = transform.position+transform.right*-4; Pet2.rotation.y = transform.rotation.y; } }
thanks for any help
Answer by Justin Warner · Apr 12, 2011 at 02:24 AM
http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
Use player prefs.
Save what the player has... Has a int 0 = not there, 1 = there.
Then you'd have, example.
//Start game, no allies;
PlayerPrefs.SetInt("dog", 0); PlayerPrefs.SetInt("friend", 0); PlayerPrefs.SetInt("cat", 0);
//Next level, dog joins;
PlayerPrefs.SetInt("dog", 1); PlayerPrefs.SetInt("friend", 0); PlayerPrefs.SetInt("cat", 0);
//Next level, dog leaves, both cat and friend comes in.
PlayerPrefs.SetInt("dog", 0); PlayerPrefs.SetInt("friend", 1); PlayerPrefs.SetInt("cat", 1);
//Etc.
Then at beginning of map, just use:
int dog = PlayerPrefs.GetInt("dog"); int friend = PlayerPrefs.GetInt("friend"); int cat = PlayerPrefs.GetInt("cat");
function Awake() { if(dog = 1){ //Spawn dog. }
if(friend = 1){ //Spawn friend. }
if(cat = 1){ //Spawn cat. } }
That should work =). Good luck!
Thanks, but still, is there any way to work around the code I'm working with?
The code you have now isn't for bringing the characters on to the next "world". It's for spawning the characters... So you'd use that code in the spot where I put comments. //Spawn [object]... So yeah, you'd use that code =).
Your answer
![](https://koobas.hobune.stream/wayback/20220613122136im_/https://answers.unity.com/themes/thub/images/avi.jpg)