- Home /
The question is answered, right answer was accepted
My game broke completely after adding player selection
Hi, I recently added player player selection in my game from which, when I click equip button the prefab it loads it's respective prefab in the main game scene. The problem is when It loads the prefab is not taking commands at all. It is not taking damage, not moving according to the joystick, camera follow(parallax effect) stopped working and game over screen is not getting triggered(SetActive). Basically everything is ruined. I have referenced everything correctly here is the screenshot: (This is a prefab and other prefabs are referenced to it) as you can this everything is referenced in the inspector. Also it is not like that, the script is not working or something else. The script was working fine before player selection. Here is the player selection scritps: write the index of the equiped object:
public class PlayerSelection : MonoBehaviour { public int CharacterIndex = 1; public GameObject Equiper; public GameObject Equiper1; public GameObject Equiper2; public GameObject Equiper3; public GameObject Equiper4; public GameObject[] characters; // Start is called before the first frame update
public void EquipIt()
{
Equip1();
SaveIndex();
}
public void EquipIt1()
{
Equip2();
SaveIndex();
}
public void EquipIt2()
{
Equip3();
SaveIndex();
}
public void EquipIt3()
{
Equip4();
SaveIndex();
}
public void EquipIt4()
{
Equip5();
SaveIndex();
}
public void Equip1()
{
CharacterIndex = 0;
}
public void Equip2()
{
CharacterIndex = 1;
}
public void Equip3()
{
CharacterIndex = 2;
}
public void Equip4()
{
CharacterIndex = 3;
}
public void Equip5()
{
CharacterIndex = 4;
}
public void SaveIndex()
{
PlayerPrefs.SetInt("CharacterIndex", CharacterIndex);
}
public void LoadIndex()
{
PlayerPrefs.GetInt("CharacterIndex", CharacterIndex);
}
}
gets the index and Instantiate player character:
public class LoadCharacter : MonoBehaviour { public Transform spawnPoint; public GameObject JetPlane1; public GameObject Grim81; public GameObject SRM98; public GameObject A1R; public GameObject TYPEP2077;
void Start()
{
int playerSelection = PlayerPrefs.GetInt("CharacterIndex");
GameObject prefab0 = JetPlane1;
GameObject prefab1 = Grim81;
GameObject prefab2 = SRM98;
GameObject prefab3 = A1R;
GameObject prefab4 = TYPEP2077;
if(playerSelection == 0)
{
GameObject clone = Instantiate(prefab0, spawnPoint.position, Quaternion.identity);
}
else if(playerSelection == 1)
{
GameObject clone1 = Instantiate(prefab1, spawnPoint.position, Quaternion.identity);
}
else if(playerSelection == 2)
{
GameObject clone2 = Instantiate(prefab2, spawnPoint.position, Quaternion.identity);
}
else if(playerSelection == 3)
{
GameObject clone3 = Instantiate(prefab3, spawnPoint.position, Quaternion.identity);
}
else if(playerSelection == 4)
{
GameObject clone4 = Instantiate(prefab4, spawnPoint.position, Quaternion.identity);
}
}
}
These are the scripts that make player character work: camera follow effect script:
public class CameraFollow : MonoBehaviour { public GameObject player; public GameObject Jet1; public GameObject Grim; public GameObject Srm; public GameObject A1R; public GameObject TypeP2077;
public Vector3 offset;
// Use this for initialization
void Start()
{
int playerSelection = PlayerPrefs.GetInt("CharacterIndex");
if(playerSelection == 0)
{
player = Jet1;
}
else if(playerSelection == 1)
{
player = Grim;
}
else if(playerSelection == 2)
{
player = Srm;
}
else if(playerSelection == 3)
{
player = A1R;
}
else if(playerSelection == 4)
{
player = TypeP2077;
}
}
// Update is called once per frame
void FixedUpdate()
{
if (player != null)
{
Vector3 camPos = transform.position;
Vector3 desiredPos = player.transform.position;
Vector3 smoothedPos = Vector3.Lerp(camPos, desiredPos, 0.125f);
transform.position = new Vector3(smoothedPos.x, transform.position.y, transform.position.z);
}
}
}
health script:
public class PlayerHealth : MonoBehaviour {
public GameObject death;
public int maxHealth = 100;
public static int currentHealth;
public HealthBar healthBar;
public GameObject gameOverPanel;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
if( currentHealth <= 0)
{
Die();
}
}
void Die ()
{
Destroy(gameObject);
audioman.PlaySound ("explosion");
Instantiate(death, transform.position, Quaternion.identity);
PlayerPrefs.SetFloat ("Highscore", PlayerPrefs.GetFloat("Highscore", 0) + ScoreScript.scoreValue);
gameOverPanel.SetActive(true);
ScoreScript.scoreValue = 0;
//script.Score.text = (ScoreScript.scoreValue).ToString();
//Time.timeScale = 0f;
}
}
movement:
public class PlaneMovement : MonoBehaviour {
public float Speed;
public float Acceleration;
public Joystick joystick;
Rigidbody2D rb;
public float RotationControl;
float MovY, MovX = 1;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
MovY = joystick.Vertical;
}
private void FixedUpdate()
{ Vector2 Vel = transform.right (MovX Acceleration); rb.AddForce(Vel);
float Dir = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right));
if(Acceleration > 0)
{
if(Dir > 0)
{
rb.rotation += MovY * RotationControl * (rb.velocity.magnitude / Speed);
}
else{
rb.rotation -= MovY * RotationControl * (rb.velocity.magnitude / Speed);
}
}
float thrustForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.down)) * 2.0f;
Vector2 relForce = Vector2.up * thrustForce;
rb.AddForce(rb.GetRelativeVector(relForce));
if(rb.velocity.magnitude > Speed)
{
rb.velocity = rb.velocity.normalized * Speed;
}
} }
I think that's all, I have other scripts but that will make the question more lengthy. So how can I fix my problem? Thanks
Your code definitely needs refactoring. Consider storing your prefab references in a list and call them by index, that way you can eli$$anonymous$$ate 80% of your repetitive methods.
Answer by Ashmit2020 · Feb 05, 2021 at 10:13 AM
It is fixed now I just added everything hierarchy and it's working.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
onapplicationquit() function is not working with unity 4.6.3 (android) 3 Answers
Save player position when trigger is destroyed 1 Answer
player prefs issues 1 Answer