I am trying to make a ship selector for my shooter game.
I have a ship selector scene and a UI that works, when selecting a ship, it changes a public int in GameSession, which doesnotdestroy. This int does change, and stays persistent on level load.
The player GO has 3 different ships in it, all not enabled, and a script to decide which one to activate.
on Load I am trying to get the Player script to check the public int in GameSession and use that to determine which ship to load.
I can not get Player script to bring back a value other than 0, even if the GameSession inspector says a number.
The only way I can get it to even register is if I put a debug.log on an Item pickup, which is obviously too late. but just to see if it even can read it.
I can’t seem to figure out how to get it to read the value on level load.
I do realize my code is atrocious. I have a lot to learn so far, I just want to make this work for now.
I have no errors related to this.
GameSession
‘’’
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class GameSession : MonoBehaviour
{
[SerializeField] int basePlayerLives = 3;
//[SerializeField] int playerLives = 3;
[SerializeField] int score = 0;
[SerializeField] float deathDelay = 3f;
[SerializeField] TextMeshProUGUI LivesText;
[SerializeField] TextMeshProUGUI ScoreText;
ScoreKeeper scoreKeeper;
LevelManager levelManager;
LivesIcons livesIcons;
ShipSelector shipSelector;
Player player;
public Canvas Hud;
public int particleWeaponAmmo = 0;
public int playerLives = 3;
public int gunLevel;
public int speedLevel = 1;
public int shipPicked = 2;
public bool Ship1Active;
public bool Ship2Active;
public bool Ship3Active;
void Awake()
{
int numGameSession = FindObjectsOfType<GameSession>().Length;
if (numGameSession > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
scoreKeeper = FindObjectOfType<ScoreKeeper>();
shipSelector = FindObjectOfType<ShipSelector>();
}
private void Start()
{
LivesText.text = playerLives.ToString();
levelManager = FindObjectOfType<LevelManager>();
Hud = GetComponentInChildren<Canvas>();
livesIcons = FindObjectOfType<LivesIcons>();
player = FindObjectOfType<Player>();
int CurrentScene = SceneManager.GetActiveScene().buildIndex;
if (CurrentScene <= 4)
{
Hud.enabled = false;
}
else if(CurrentScene >=5)
{
Hud.enabled = true;
}
}
public void ProcessPlayerDeath()
{
if (playerLives > 1)
{
Invoke("TakeLife", deathDelay);
}
else
{
Invoke("ResetGameSession", deathDelay);
}
}
public void ProcessBossDeath()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
Invoke("FinishedGame", deathDelay);
}
else
{
levelManager.LoadNextLevel();
}
}
public void GoBackToMenu()
{
Invoke("BackToMenu", 0);
}
public void AddToScore(int pointsToAdd)
{
score += pointsToAdd;
ScoreText.text = score.ToString();
}
void TakeLife()
{
playerLives--;
livesIcons.ProcessPlayerLivesIcons();
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
LivesText.text = playerLives.ToString();
}
void ResetGameSession()
{
SceneManager.LoadScene("GameOver");
ResetLives();
LivesText.text = playerLives.ToString();
livesIcons.ProcessPlayerLivesIcons();
Hud.enabled = false;
}
void FinishedGame()
{
ResetLives();
SceneManager.LoadScene("FinishedGame");
LivesText.text = playerLives.ToString();
Hud.enabled = false;
}
void BackToMenu()
{
ResetLives();
Time.timeScale = 1f;
SceneManager.LoadScene("MainMenu");
Hud.enabled = false;
LivesText.text = playerLives.ToString();
}
void Update()
{
ScoreText.text = scoreKeeper.GetScore().ToString("000000000");
}
public void ResetLives()
{
playerLives = basePlayerLives;
FindObjectOfType<GameSession>().gunLevel = 0;
}
public void ShipSelected()
{
levelManager.LoadFirstLevel();
}
public void ShipSelecteddel()
{
if(Ship1Active == true)
{
Debug.Log("Ship 1 Selected");
Ship1Active = true;
Ship2Active = false;
Ship3Active = false;
levelManager.LoadFirstLevel();
return;
}
if (Ship2Active == true)
{
Debug.Log("Ship 2 Selected");
Ship1Active = false;
Ship2Active = true;
Ship3Active = false;
levelManager.LoadFirstLevel();
return;
}
if (Ship3Active == true)
{
Debug.Log("Ship 3 Selected");
Ship1Active = false;
Ship2Active = false;
Ship3Active = true;
levelManager.LoadFirstLevel();
return;
}
}
public void Ship1PickedGS()
{
Debug.Log("Ship 1 Selected");
Ship1Active = true;
Ship2Active = false;
Ship3Active = false;
levelManager.LoadFirstLevel();
return;
}
public void Ship2PickedGS()
{
Debug.Log("Ship 2 Selected");
Ship1Active = false;
Ship2Active = true;
Ship3Active = false;
levelManager.LoadFirstLevel();
return;
}
public void Ship3PickedGS()
{
Debug.Log("Ship 3 Selected");
Ship1Active = false;
Ship2Active = false;
Ship3Active = true;
levelManager.LoadFirstLevel();
return;
}
}
‘’’
Player
‘’’
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
public float moveSpeed = 10f;
Vector2 rawInput;
[SerializeField] float paddingLeft;
[SerializeField] float paddingRight;
[SerializeField] float paddingTop;
[SerializeField] float paddingBottom;
[SerializeField] ParticleSystem particleWeaponParticles;
[SerializeField] AudioClip particleWeaponSFX;
[SerializeField] GameObject ship1;
[SerializeField] GameObject ship2;
[SerializeField] GameObject ship3;
public int playerShipPicked;
[SerializeField] UnityEngine.GameObject pauseMenuUI;
public static bool GameIsPaused = false;
GunLevel gunLevel;
SpeedLevel speedLevel;
PWeaponAmmoIcons pWeaponAmmoIcons;
LevelManager levelManager;
Vector2 minBounds;
Vector2 maxBounds;
Shooter shooter;
Gun[] guns;
AudioPlayer audioPlayer;
GameSession gameSession;
bool isFiring;
bool ship1ToGo;
bool ship2ToGo;
bool ship3ToGo;
private void Awake()
{
gameSession = FindObjectOfType<GameSession>();
ActivateSelectedShip();
levelManager = FindObjectOfType<LevelManager>();
Debug.Log(gameSession.shipPicked);
}
private void Start()
{
InitBounds();
shooter = GetComponent<Shooter>();
guns = transform.GetComponentsInChildren<Gun>();
gunLevel = FindObjectOfType<GunLevel>();
speedLevel = FindObjectOfType<SpeedLevel>();
pWeaponAmmoIcons = FindObjectOfType<PWeaponAmmoIcons>();
audioPlayer = FindObjectOfType<AudioPlayer>();
Debug.Log("Insert load method here");
Debug.Log("Player ship Picked " + playerShipPicked);
}
void Update()
{
Move();
guns = transform.GetComponentsInChildren<Gun>();
//gameSession = FindObjectOfType<GameSession>();
}
private void OnEnable()
{
}
void InitBounds()
{
Camera mainCamera = Camera.main;
minBounds = mainCamera.ViewportToWorldPoint(new Vector2(0, 0));
maxBounds = mainCamera.ViewportToWorldPoint(new Vector2(1, 1));
}
private void Move()
{
Vector3 delta = rawInput * moveSpeed * Time.deltaTime;
Vector2 newPos = new Vector2();
newPos.x = Mathf.Clamp(transform.position.x + delta.x, minBounds.x + paddingLeft, maxBounds.x - paddingRight);
newPos.y = Mathf.Clamp(transform.position.y + delta.y, minBounds.y + paddingBottom, maxBounds.y - paddingTop);
transform.position = newPos;
}
void OnMove(InputValue value)
{
rawInput = value.Get<Vector2>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "SpeedBoost")
{
Debug.Log("Boost it!");
speedLevel.ProcessSpeedLevel();
Destroy(other.gameObject);
}
if (other.tag == "FireRateUp")
{
Debug.Log("Blast 'Em!!!");
gunLevel.ProcessGunLevel();
Destroy(other.gameObject);
}
}
void OnFire(InputValue value)
{
if (guns != null)
{
foreach (Gun gun in guns)
{
gun.isFiring = value.isPressed;
}
}
Debug.Log("Firing");
}
void OnFire2(InputValue value)
{
Debug.Log("Particle Weapon Button Pressed");
if (gameSession.particleWeaponAmmo >= 1)
{
Debug.Log("Firing Super Weapon");
gameSession.particleWeaponAmmo--;
Instantiate(particleWeaponParticles, transform.position, Quaternion.identity);
audioPlayer.PlayParticleWeaponClip();
Debug.Log("Particle Weapon Ammo: " + gameSession);
pWeaponAmmoIcons.ProcessPWeaponAmmo();
}
Debug.Log("No Particle Weapon Ammo Left");
return;
}
void OnPause()
{
Debug.Log("Pause");
if (GameIsPaused)
{
Resume();
Debug.Log("Resuming");
}
else
{
Pause();
Debug.Log("Pausing");
}
}
public void Resume()
{
Debug.Log("Resume???");
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
public void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
public void ActivateSelectedShip()
{
Debug.Log("Activating Selected Ship");
Debug.Log(playerShipPicked);
int ThisShip = gameSession.shipPicked;
if (playerShipPicked == 0)
{
Debug.Log("Ship 1 to go by default");
ActivateShip1();
}
if (ThisShip == 1)
{
Debug.Log("Ship 1 to go");
ActivateShip1();
}
else if (ThisShip == 2)
{
Debug.Log("Ship 2 to go");
ActivateShip2();
}
else if (ThisShip == 3)
{
Debug.Log("Ship 3 to go");
ActivateShip3();
}
}
public void ActivateShip1()
{
ship1.gameObject.SetActive(true);
Debug.Log("Ship 1 should work now..?");
ship2.gameObject.SetActive(false);
ship3.gameObject.SetActive(false);
return;
}
public void ActivateShip2()
{
ship1.gameObject.SetActive(false);
ship2.gameObject.SetActive(true);
Debug.Log("Ship 2 should work now..?");
ship3.gameObject.SetActive(false);
return;
}
public void ActivateShip3()
{
ship1.gameObject.SetActive(false);
ship2.gameObject.SetActive(false);
ship3.gameObject.SetActive(true);
Debug.Log("Ship 3 should work now..?");
return;
}
}
‘’’