Game Object not activating.

Hi,

I am making a space invaders type game and in this game there are 3 ships for players to choose from.
When we start the game, we are greeted with a menu screen where we are instructed to choose one of the three ships.
There are three buttons carrying the respective ship’s name when user clicks on one of the button that ship is used throughout the game.

On the script side

There is a GUI_Control.cs script which receives the input to the button.
There are 3 public GameObject variables (ship0,ship1,ship2).
Each ship is assigned an unique code (ship0 = 0, ship1 = 1, ship2 = 2)
For example,
If the user clicks the ship2 button a function is called which does this—>

PlayerPrefs.SetInt("Ship",2);

Then the game starts and loads the first level.
There a script named ShipView.cs receives the PlayerPref and does this—>

public GameObject ship0,ship1,ship2;
int ship = 0;

void Start()
{
  ship = PlayerPrefs.GetInt("ship");
  
  if(ship == 0) ship0.active = true;
  else if(ship == 1) ship1.active = true;
  else if(ship == 2) ship2.active = true;
}

Now the problem is that when I choose ship2 the game starts but my ship is still inactive.
While with selecting ship0 or ship1 it works fine.

I also tried using Debug.Log() to print which areas of my code ran and it seems that every thing works fine because my variables are all properly set and even the ship2.active = true is executed but it has no effect my game object is still disabled.

Please Help!

1 Answer

1

The “active” property of the class GameObject is deprecated.
Try using the function gameObject.SetActive(true); instead.

Also you are setting the PlayerPrefs “Ship” with an uppercase S, and getting the value “ship”, with a lowercase s. I do think the PlayerPrefs are case sensitive, so try changing those to be the same as well.

Yep, changing the active to SetActive() worked!. Thanks!