Help I've Lost all Hope, A script stopped working on my Player Character Prefab!
Hi Gamedevs, First time posting here.. so i ran out of search sentences to type on Google, so im here and very desperate. really hope someone replies. this is my first game project intended for android. so i have to scripts listed below both of which control various aspects of my character. the problem im facing is the player script i had which was working totally fine suddenly stopped working partially when the player dies and is instantiated by a game master script.. by partial i mean the ontrigger and collision functions and start functions work. but update has stopped working weirdly.. initially thought this was because of a crash so i deleted the cache items of the project and it rebuilt all but unfortunately it persists. what drives me crazy is that i keep reading the script over and over but cant see why my update just outright wont function even after saving it on the prefab! here is the script in question.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player_0 : MonoBehaviour
{
Animator anim;
Animator shieldan;
Animator enani;
SpriteRenderer sprite;
bool damaged;
GameMaster gm;
public GameObject en_barry;
Image image;
public bool shield = false;
public float MaxshieldTime = 10f;
public float ShieldTimer;
public int MaxHealth;
public int MaxEnergy;
public GameObject shield_fx;
public GameObject injurefx;
public GameObject explosive;
bool bullet_hit;
bool slashed;
public float recharge_per_second;
public GameObject heart_fx;
bool collect;
public GameObject collect_fx;
Shake_the_Camera camerashake;
AudioSource audioz;
public AudioClip[] injure_sounds;
AudioClip injury;
public AudioClip hit_by_insect;
public AudioClip[] collector_sounds;
AudioClip collect_sounds;
public AudioClip energy_sound;
public AudioClip crunch_sounds;
[System.Serializable]
public class PlayerStats
{
public int Health = 100;
public float Energy = 100;
}
public PlayerStats playerStats = new PlayerStats();
public int FallBoundary = -80;
private void Start()
{
anim = GetComponent<Animator>();
sprite = GetComponent<SpriteRenderer>();
shieldan = shield_fx.GetComponent<Animator>();
playerStats.Health = MaxHealth;
playerStats.Energy = MaxEnergy;
image = en_barry.GetComponent<Image>();
camerashake = GameObject.FindGameObjectWithTag("GM").GetComponent<Shake_the_Camera>();
enani = en_barry.GetComponent<Animator>();
audioz = GetComponent<AudioSource>();
}
public void Update()
{
if (image == null)
{
image = GameObject.FindGameObjectWithTag("energybar").GetComponent<Image>();
}
if (damaged == true)
{
StartCoroutine("damageflicker");
damaged = false;
}
//check to prevent character from endless falling
if (transform.position.y <= FallBoundary)
{ DamagePlayer(100); }
audioscrambler();
S_timer();
Show_energy_Level();
Recharge_Energy();
particle_handler();
}
void audioscrambler()
{
injury = injure_sounds[Random.Range(0, injure_sounds.Length)];
}
void S_timer()
{
if (shield == true)
{
ShieldTimer -= Time.deltaTime;
if (ShieldTimer <= 4f)
{
shieldan.SetBool("timeup", true);
}
if (ShieldTimer <= 0)
{
shieldan.SetBool("timeup", false);
shield = false;
shield_fx.SetActive(false);
}
}
}
void DamagePlayer(int damage)
{
playerStats.Health -= damage;
if (playerStats.Health <= 0)
{
GameMaster.KillPlayer(this);
}
}
void Show_energy_Level()
{
image.fillAmount = (playerStats.Energy / MaxHealth);
if (playerStats.Energy <= 30)
{
enani.SetBool("valueless", true);
}
else if (playerStats.Energy > 20)
{
enani.SetBool("valueless", false);
}
}
void Recharge_Energy()
{
if (playerStats.Health > MaxHealth)
{
playerStats.Health = MaxHealth;
}
if (playerStats.Energy < MaxEnergy)
{
playerStats.Energy += (recharge_per_second * Time.maximumDeltaTime);
Debug.Log("registering energy delta");
}
else if (playerStats.Energy > MaxEnergy)
{
playerStats.Energy = MaxEnergy;
enani.SetBool("valuefull", true);
Invoke("energyisfull", 2f);
}
}
void particle_handler()
{
if (collect == true)
{
collect = false;
collect_sounds = collector_sounds[Random.Range(0, collector_sounds.Length)];
GameObject col = Instantiate(collect_fx, transform.position, transform.rotation) as GameObject;
Debug.Log("added sound effect");
audioz.PlayOneShot(collect_sounds, 0.7f);
collect = false;
}
if (bullet_hit == true)
{
Instantiate(explosive, transform.position, transform.rotation);
audioz.pitch = Random.Range(1f, 1.5f);
audioz.PlayOneShot(hit_by_insect, 0.8f);
bullet_hit = false;
}
else { audioz.pitch = 1f; }
if (slashed == true)
{
GameObject sl = Instantiate(injurefx, transform.position, transform.rotation) as GameObject;
Debug.Log("slashed anim is playing");
audioz.PlayOneShot(injury, 1f);
slashed = false;
}
}
IEnumerator damageflicker()
{
if (damaged == true)
{
float wait = 0.10f;
sprite.color = new Color(1f, 0f, 0f);
yield return new WaitForSeconds(wait);
sprite.color = new Color(1f, 1f, 1f);
yield return new WaitForSeconds(wait);
sprite.color = new Color(1f, 0f, 0f);
yield return new WaitForSeconds(wait);
sprite.color = new Color(1f, 1f, 1f);
yield return new WaitForSeconds(wait);
sprite.color = new Color(1f, 0f, 0f);
yield return new WaitForSeconds(wait);
sprite.color = new Color(1f, 1f, 1f);
yield return new WaitForSeconds(wait);
sprite.color = new Color(1f, 0f, 0f);
yield return new WaitForSeconds(wait);
sprite.color = new Color(1f, 1f, 1f);
yield return new WaitForSeconds(1f);
}
// else { StopCoroutine(damageflicker()); }
}
//Collider detection via Trigger to damage player based on three injury levels
private void OnTriggerEnter2D(Collider2D trigger)
{
if (trigger.gameObject.tag == "shield")
{
Destroy(trigger.gameObject);
shield_fx.SetActive(true);
ShieldTimer = MaxshieldTime;
shield = true;
collect = true;
}
if (trigger.gameObject.tag == "addlife")
{
Debug.Log("collided with coin send to bank");
Destroy(trigger.gameObject);
GameMaster.AddMoney(this);
collect = true;
}
if (trigger.gameObject.tag == "addhealth")
{
Debug.Log("collided with coin send to bank");
Destroy(trigger.gameObject);
playerStats.Health += 10;
collect = true;
audioz.PlayOneShot(crunch_sounds, 0.6f);
Instantiate(heart_fx, transform.position, transform.rotation);
}
if (trigger.gameObject.tag == "addstones")
{
Debug.Log("collided with stone send to bank");
Destroy(trigger.gameObject);
GameMaster.AddStones(this);
collect = true;
}
if (trigger.gameObject.tag == "addenergy" && playerStats.Energy < MaxEnergy)
{
Debug.Log("collided with coin send to bank");
Destroy(trigger.gameObject);
playerStats.Energy += 20;
audioz.PlayOneShot(energy_sound, 0.4f);
collect = true;
}
if ((trigger.gameObject.tag == "enemyTier_1") && (shield == false))
{
damaged = true;
DamagePlayer(2);
}
if (trigger.gameObject.tag == "enemyTier_2" && (shield == false))
{
damaged = true;
DamagePlayer(10);
slashed = true;
}
if (trigger.gameObject.tag == "enemyTier_3" && (shield == false))
{
damaged = true;
DamagePlayer(20);
slashed = true;
}
}
private void OnTriggerExit2D(Collider2D trigger)
{
if (trigger.gameObject.tag == "enemyTier_1")
{
damaged = false;
}
if (trigger.gameObject.tag == "enemyTier_2")
{
damaged = false;
}
if (trigger.gameObject.tag == "enemyTier_3")
{
damaged = false;
}
}
//Collision detection via collision2d to damge player based on 3 injury levels
private void OnCollisionEnter2D(Collision2D trigger)
{
if (trigger.gameObject.tag == "enemybullets" && (shield == false))
{
damaged = true;
DamagePlayer(6);
bullet_hit = true;
camerashake.Shake(0.03f, 0.1f);
}
if (trigger.gameObject.tag == "enemyrocks" && (shield == false))
{
damaged = true;
DamagePlayer(6);
bullet_hit = true;
camerashake.Shake(0.04f, 0.1f);
}
else if (trigger.gameObject.tag == "enemyTier_2" && (shield == false))
{
damaged = true;
slashed = true;
DamagePlayer(15);
}
else if (trigger.gameObject.tag == "enemyTier_3" && (shield == false))
{
damaged = true;
slashed = true;
DamagePlayer(25);
}
}
/* private void OnCollisionExit2D(Collision2D trigger)
{
if ((trigger.gameObject.tag == "enemybullets"|| trigger.gameObject.tag == "enemyTier_2"|| trigger.gameObject.tag == "enemyTier_3"))
{
damaged = false;
}
}*/
void energyisfull()
{
enani.SetBool("valuefull", false);
}
}
here is my other player control script,
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine;
public class AdamControl : MonoBehaviour
{
//how fast player can move
public float topspeed = 10.3f;
float speed;
//how fast the player falls for shockwave attack
public float fallspeed = 5f;
public bool sliding = false;
float slidetimer;
public float maxslidetime = 1f;
public float Dashspeed = 100f;
bool shocktimer = false;
bool noshock;
//get reference to animator
Animator anim;
//reference to rigidbody
Rigidbody2D reggie;
//when not grounded
bool grounded = false;
//get axis movement variable
//tell the sprite which direction it is pointing
public float move;
//variable to check double jump
bool doublejump = false;
public GameObject dash;
public Transform dashpoint;
public GameObject shock;
public GameObject shock_particle_fx;
public Transform shockpoint;
CapsuleCollider2D enable_dash;
Player_0 playerStat;
public GameObject charge_fx;
bool chargefx;
//dash animation
//transform at character feet to see if he is touching the ground
public Transform groundCheck;
//how big the circle is going to be when we check the distance with the ground
float groundRadius = 0.2f;
//what layer is actually the ground?
public LayerMask whatisground;
public float GroundRadius
{
get
{
return groundRadius;
}
set
{
groundRadius = value;
}
}
//force oof the jump
public float jumpForce = 700f;
Shake_the_Camera camerashake;
public float shakeamount;
AudioSource audi;
//soundfx
public AudioClip walk_sound;
public AudioClip run_sound;
public AudioClip[] random_jump;
AudioClip jump_twice;
public AudioClip land;
public AudioClip dash_sound;
public AudioClip shock_sound;
public AudioClip charging;
void Start()
{
camerashake = GameObject.FindGameObjectWithTag("GM").GetComponent<Shake_the_Camera>();
enable_dash = GameObject.FindGameObjectWithTag("dash_attack").GetComponent<CapsuleCollider2D>();
anim = GetComponent<Animator>();
reggie = GetComponent<Rigidbody2D>();
speed = topspeed;
playerStat = GetComponent<Player_0>();
audi = GetComponent<AudioSource>();
}
//physics is fixed update
public void FixedUpdate()
{
//reset double jump
{
if (grounded)
doublejump = false;
}
//get move direction
//move = (CrossPlatformInputManager.GetAxisRaw("Horizontal"));
move = Input.GetAxis("Horizontal");
//bool statement to find if grounf radius touched what is ground mask
grounded = Physics2D.OverlapCircle(groundCheck.position, GroundRadius, whatisground);
//tell animator we are on ground from above statement
anim.SetBool("Ground", grounded);
//get how fast we are moving from the rigid body
anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
//add velocity to the rigid body in move direction* our speed
reggie.velocity = new Vector2(move * topspeed, reggie.velocity.y);
anim.SetFloat("speed", Mathf.Abs(move));
//if we are not facing the negative direction and not facing right flip
//as of 5.3 unity update flip is native in sprite renderer
if (move > 0)
{ GetComponent<SpriteRenderer>().flipX = false; }
else if (move < 0)
{ GetComponent<SpriteRenderer>().flipX = true; }
}
void Update()
{
GetInputMovement();
sound_randomer();
}
public void sound_randomer()
{
jump_twice = random_jump[Random.Range(0, random_jump.Length)];
}
public void GetInputMovement()
{
if ((grounded || !doublejump) && (/*CrossPlatformInputManager.GetButtonDown("jumpy") ||*/ Input.GetKeyDown(KeyCode.UpArrow)))
{
//nojump
anim.SetBool("Ground", false);
//add jumpforce to the yaxis of the rigid body
reggie.AddForce(new Vector2(0, jumpForce));
audi.PlayOneShot(jump_twice, 0.1f);
if (!doublejump && !grounded)
{
doublejump = true;
}
}
if (Input.GetKeyDown(KeyCode.RightShift) && doublejump && shocktimer == false && playerStat.playerStats.Energy >= 35)
{
//perform shockwave attack
playerStat.playerStats.Energy -= 40;
reggie.velocity += Vector2.up * Physics2D.gravity.y * (fallspeed - 1) * Time.deltaTime;
anim.SetBool("Shockwave", true);
topspeed = 0;
shocktimer = true;
}
if (grounded)
{
if (shocktimer == true)
{
Instantiate(shock, shockpoint.position, shockpoint.rotation);
Instantiate(shock_particle_fx, groundCheck.position, groundCheck.rotation);
shocktimer = false;
camerashake.Shake(shakeamount, 1f);//camerashake
audi.PlayOneShot(shock_sound, 0.85f);//explosion audio
}
}
bool noidle;
if (anim.GetCurrentAnimatorStateInfo(0).IsName("idle"))
{
anim.SetBool("Shockwave", false);
charge_fx.SetActive(false);
topspeed = speed;
noidle = true;
}
else { noidle = false; }
if (Input.GetKeyDown(KeyCode.RightShift) && grounded && (sliding == true) && (reggie.velocity.y > -1 && reggie.velocity.y < 1) && playerStat.playerStats.Energy >= 10 && noidle ==false)
{
topspeed = 6f;
chargefx = true;
noshock = true;
audi.PlayOneShot(charging, 0.20f);
charge_fx.SetActive(true);
}
if (Input.GetKeyUp(KeyCode.RightShift) && grounded && (sliding == true) && (reggie.velocity.y > -1 && reggie.velocity.y < 1) && noshock == true && playerStat.playerStats.Energy >= 15 && noidle == false)
{
enable_dash.enabled = true;
anim.SetBool("IsSliding", true);
Debug.Log("slide logic reached");
camerashake.Shake(shakeamount, 0.12f);
if (move > 0)
{
reggie.MovePosition(transform.position + transform.right * Dashspeed);
Instantiate(dash, dashpoint.position, dashpoint.rotation);
}
else if (move < 0)
{
reggie.MovePosition(transform.position + transform.right * -Dashspeed);
Instantiate(dash, dashpoint.position, dashpoint.rotation);
}
playerStat.playerStats.Energy -= 13f;
sliding = false;
slidetimer = maxslidetime;
topspeed = speed;
noshock = false;
charge_fx.SetActive(false);
audi.PlayOneShot(dash_sound, 0.4f);
}
if (sliding == false)
{
slidetimer -= Time.deltaTime;
if (slidetimer <= 0)
{
sliding = true;
enable_dash.enabled = false;
anim.SetBool("IsSliding", false);
}
}
}
public void footstepswalk()
{
if (noshock == false)
{
audi.PlayOneShot(walk_sound, 1f);
}
}
public void footstepsrun()
{
if (noshock == false)
{
audi.PlayOneShot(run_sound, 1f);
}
}
public void onground()
{
audi.PlayOneShot(land, 0.65f);
}
}
and here is the gamemaster;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class GameMaster : MonoBehaviour {
public static GameMaster gm;
TextMeshProUGUI text;
TextMeshProUGUI text2;
public GameObject lives_counter;
public GameObject lives_counter_fx;
public GameObject lives_counter_add_fx;
public GameObject white_explode;
bool lives_counter_add_check = false;
public GameObject gems_counter;
public GameObject gems_counter_fx;
bool gem_counter_add_check;
Player_0.PlayerStats playerStats;
[System.Serializable]
public class GameStats
{
public int Lives = 3;
public int Stones = 0;
}
public GameStats gameStats = new GameStats();
void Start()
{
if(gm == null)
{
gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
}
text = lives_counter.GetComponent<TextMeshProUGUI>();
text2 = gems_counter.GetComponent<TextMeshProUGUI>();
text.text = gameStats.Lives.ToString();
text2.text = gameStats.Stones.ToString();
}
private void Update()
{
lives_gems_particle_controller();
}
void lives_gems_particle_controller()
{
text.text = gameStats.Lives.ToString();
if (gameStats.Lives <= 0)
{
Debug.Log("reload scene!");
}
text2.text = gameStats.Stones.ToString();
if (lives_counter_add_check == true)
{
lives_counter_add_fx.SetActive(true);
StartCoroutine(waitforturnparticle());
lives_counter_add_check = false;
}
if (gem_counter_add_check == true)
{
gems_counter_fx.SetActive(true);
StartCoroutine(waitforgemparticle());
gem_counter_add_check = false;
}
}
IEnumerator waitforturnparticle()
{
yield return new WaitForSeconds(3.50f);
lives_counter_add_fx.SetActive(false);
StopCoroutine(waitforturnparticle());
}
IEnumerator waitforgemparticle()
{
yield return new WaitForSeconds(1.30f);
gems_counter_fx.SetActive(false);
StopCoroutine(waitforgemparticle());
}
public Transform ReSpawn;
public GameObject PlayerPrefab;
public Transform spawnprefab;
public float spawndelay = 0f;
[HideInInspector]
public bool spawncheck;
public IEnumerator Respawnplayer()
{
yield return new WaitForSeconds(spawndelay);
if (spawncheck == true)
{
spawncheck = false;
Instantiate(PlayerPrefab, ReSpawn.position, ReSpawn.rotation);
Transform clone = Instantiate(spawnprefab, ReSpawn.position, ReSpawn.rotation) as Transform;
Destroy(clone.gameObject, 8f);
gameStats.Lives = gameStats.Lives - 1;
lives_counter_fx.SetActive(true);
text.text = gameStats.Lives.ToString();
}
}
public static void KillPlayer(Player_0 player)
{
Destroy(player.gameObject);
gm.spawncheck = true;
// Instantiate(gm.white_explode, player.transform.position, player.transform.rotation);
gm.StartCoroutine (gm.Respawnplayer());
gm.lives_counter_fx.SetActive(false);
}
public static void KillEnemy (Enemydamage enemy)
{
Destroy(enemy.transform.parent.gameObject,0.10f);
}
public static void AddMoney (Player_0 player)
{
gm.gameStats.Lives = gm.gameStats.Lives + 1;
gm.lives_counter_add_check = true;
}
public static void checkpost (checkpoint checkpoint)
{
gm.ReSpawn.position = checkpoint.transform.position;
}
public static void AddStones (Player_0 player)
{
gm.gameStats.Stones += 1;
gm.gem_counter_add_check = true;
}
}
I have script that handles the camera zoom based on the player position so ill save your time by not posting it.. but any thing you need to understand whats wrong please tell me ! i desperate and i fear this project of more than 6 months may be a failure.