- Home /
Selected object into next scene?
Alright. So I have two scenes:
Main Menu
Game
In the Main Menu I have a character select screen where I can pick the character I want to use. That part works just fine. I select the GameObject and an animation plays on the selected Object. When I've selected the character I want, I'll press "Play" which loads the Game scene.
But I just can't get that specific character to be shown in the next scene. I've already placed all the characters on top of each other, and want to destroy all the ones that was "not selected". I've tried making the SelectedCharacter GameObject Static, but with no luck. I just can't wrap my head around on how to make this work.
Any tips, ideas or whatever on what I should do? I'm completely stuck.
Answer by Owen-Reynolds · Mar 21, 2013 at 02:11 PM
Can use a persistent "data" object to remember which char you want, then reference that in the next scene. For example (on an empty): class charStats { public int charNum; public int gold; ... }
Then in your menu script somewhere would have things like: GameObject.Find("CharData").GetComponent().charNum==2;
In your game scenes, each character could then check to see if it had the correct charNum: if(GO.Find("CharData")....charNum != myCharNum) Destroy(me)
The CharData gameObject with the script would be the only persistant (dontDestroyOnLoad.)
Alternative is to have the character be persistent. The (minor) problem there is you need to mark the starting spot for each scene and "teleport" the character there on scene load (since the new scene probably won't start at the exact xyz the last one ended.)
Answer by GuyTidhar · Mar 21, 2013 at 01:00 PM
Have you tried:
http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
If you do:
DontDestroyOnLoad(rootGameObject);
Loading a new scene won't destroy the root game objects and all its child game objects and components.
Answer by Watercolours · Mar 21, 2013 at 08:29 PM
Alright, so I actually figured it out on my own. I somewhat did what Owen Reynolds said, but I used `PlayerPrefs` instead. Works like a charm and does exactly what I want. Can't believe I didn't know about it before. So much hassle for something so super simple.
Thanks for helping out guys!
Hey,
I know you posted this quite some time ago, is it possible you could share your solution? I'm doing something similar where I have a player selection menu, I then have my new scene where I want the selected character to appear based on the users choice from previous scene.
Thanks a lot.
You're right, this is quite old. And I don't understand why you had to ask that question when it's clearly answered here. Both in the post you've commented on and by Owen Reynolds. So please, just read..
You just said you used player prefs actually, I'm really new to unity so saying you used player prefs other than reading about it in documentation means nothing to me. You don't need to be so rude when this is meant to be a community forum where you help others. $$anonymous$$ bye
If reading the documentation doesn't mean anything to you, then you should probably get someone else to do the coding (or begin learning some other language with better APIs). But! since I'm in a good mood today I'll tell you the basics which are pretty obvious here.
In your CharacterSelector
you want to assign all characters an Integer, get the one you pick from the menu and set it with PlayerPrefs like this:
PlayerPrefs.SetInt("CharacterInteger", chosenCharacterNumber);
and when you get a new scene you simply load it with:
PlayerPrefs.GetInt("CharacterInteger", chosenCharacterNumber);
then destroy the ones that weren't chosen. It's not the best method, but it works really well. I used it for a project in College and haven't used Unity since then so I'm a bit rusty, but this is pretty much all you need for a basic Character Selector with scene change.
Thanks a lot for your help. Sadly i cant get anyone else to code as I'm doing a project in school.. lol.
Will test this out now and see how it goes :)
Your answer
Follow this Question
Related Questions
Character selection system help script 2 Answers
sliding character selection menu 0 Answers
Any ideas to do a character selection 4 Answers
Char select / GUI button question. 3 Answers
Switching between multiple classes 2 Answers