- Home /
Trying to destory a character on a different scene with a UI button
Hello there, rookie here, I am having issues with my current code. I am currenly making a character select screen and want to have the other characters already loaded into the game on the main game screen.
On the start menu there will be two buttons that will allow you to be able to pick between one of the two charatcers and upon interaction with one of the buttons, will destory the character that wasnt chosen on the next scene and the game will be played.
This is what i have so far and thank you for the hlep in advance :) !
Answer by JxWolfe · Jan 02, 2019 at 10:18 PM
Basically if you need to set a variable telling which character is chosen, then make that script variable static. Then when you load your scene check that static variable to destroy the other player.
Ok, looking at your code there is one BIG issue... Your making a class INSIDE another class. Two options, either do this,
public static class GlobalVariables
{
public bool characterChoice;
}
public class Elane_Button : MonoBehaviour {
void Start()
{
if(GlobalVariables.characterChoice == true)
{
//do something
}
}
}
or
public class Elane_Button : MonoBehaviour {
public static bool characterChoice;
void Start()
{
if(characterChoice == true) //maybe Elane_Button.characterChoice if that doesn't work
{
//do something
}
}
}
Either of those approaches SHOULD work... I may have messed up -- Hope it helps, again please ask if you need more assistance.
ok, so here's an idea that I had... If you ever plan of expanding your characterChoices for you game, you may wish to change characterChoice into a enum... that way you could have thousands of characters without changing your variable... if you like that idea, I will be happy to supply the code.
right I scrapped my code and have re-writen it and its works till the "Destroy(Player)". From what I know and from what unity is telling me, I can't use "Player" because it is a "type that cannot be given context" im not sure what I should put in the brackets as I know if I put "gameObject" I will then have to attach the script to the object in that same scene, which it isnt in. Or am I also right in saying that i can attatch the script to the player even though it is in a different scene and still call it "gameObject"?
ok, here's what I would do... On your character's script -- add this command in your Start()
public bool defaultCharacter;
//basicly if characterChoice == true, then it's selecting our character if defaultCharacter == true;
void Start()
{
if(Elane_Button.characterChoice != defaultCharacter)
{
//it says that the characterChoice isn't our character
Destroy(gameObject);
}
}
So that needs to go in the CHARACTER'S SCRIPT... because we can reference a static variable without any additional steps.
ok yeah that is working now, the only issue i have now is that when I attach the "Elane_button" script, I need to then link that to the button which i have done, but I am unsure about is what on event click I should be using. The image below is what I mean, had to get another user to upload it.
Answer by Shaolin-Dave · Jan 03, 2019 at 10:35 PM
You've provided code that shows it's checking the value of your static bool, but not anything that shows the bool ever being populated. You should share your button code. Also, add debug logs to make sure that the bool is both being set and read properly. I have a feeling it's checking for true/false values but actually finding null.
Beyond that, this isn't a very good way to select characters: My suggestion is to have your button populate and enum or select a prefab GameObject from an array of available characters. Create a "spawner" GameObject that will spawn the one character you want to play as, not a script to destroy the ones you don't want.
void Start()
{
Debug.Log(“characterChoice = “ + characterChoice);
if (!characterChoice) {
// Instantiate Player2 GameObject Here
} else {
// Instantiate Player1 (default) GameObject Here
}
}
I did try to make a character select screen before using a "spawner" but came t the issue that one of my scripts would not link itself to the characater on that scene as it did not exist on that scene, even using a prefab, it still never worked, hence why i am trying to do it as above.
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D(Collider2D other) 2 Answers
Add audio. 1 Answer
2d platformer script problems 0 Answers
"Create New Palette" doesn't actually create a palette at all 1 Answer
Cinemachine camera shake on button press 0 Answers