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 :slight_smile: !

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.

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
    	}
    }

@JxWolfe Hi there, i have tried to make the varaible a static bool variable but unity will not detect it for some reason, i feel like i have nit set it right? (see pic in post)

Try using “==” instead of “=”