- Home /
When my player dies and i hit revive button i want my player back to the game but i m unable to do that please help.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Advertisements; using System;
public class GameManager : MonoBehaviour {
private const int COIN_SCORE_AMOUNT = 5;
public static GameManager Instance { set; get; }
public bool IsDead { set; get; }
private bool isGameStarted = false;
private PlayerMotor motor;
// UI and UI feilds
public Animator gameCanvas, menuAnim, diamondAnim;
public Text scoreText, coinText, modifierText, highscoreText;
private float score, coinScore, modifierScore;
private int lastScore;
// Death Menu
public Animator deathMenuAnim;
public Text deadscoreText, deadcoinText;
private void Awake()
{
Instance = this;
modifierScore = 1;
motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor>();
modifierText.text = "x" + modifierScore.ToString("0.0");
coinText.text = coinScore.ToString("0");
scoreText.text = score.ToString("0");
highscoreText.text = PlayerPrefs.GetInt("Highscore").ToString();
// Advertisement
Advertisement.Initialize("3061978");
}
private void Update()
{
if (MobileInput.Instance.Tap && !isGameStarted)
{
isGameStarted = true;
motor.StartRunning();
FindObjectOfType<GlacierSpawners>().IsScrolling = true;
FindObjectOfType<CameraMotor>().IsMoving = true;
gameCanvas.SetTrigger("Show");
menuAnim.SetTrigger("Hide");
}
if(isGameStarted && !IsDead)
{
// Bump the score up
score += (Time.deltaTime * modifierScore);
if(lastScore != (int)score)
{
lastScore = (int)score;
scoreText.text = score.ToString("0");
}
}
}
public void GetCoin()
{
diamondAnim.SetTrigger("Collect");
coinScore++;
coinText.text = coinScore.ToString("0");
score += COIN_SCORE_AMOUNT;
scoreText.text = scoreText.text = score.ToString("0");
}
public void UpdateModifier(float modifierAmount)
{
modifierScore = 1.0f + modifierAmount;
modifierText.text = "x" + modifierScore.ToString("0.0");
}
public void OnPlayButton()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("MainScene");
}
public void OnDeath()
{
IsDead = true;
FindObjectOfType<GlacierSpawners>().IsScrolling = false;
deadscoreText.text = score.ToString("0");
deadcoinText.text = coinScore.ToString("0");
deathMenuAnim.SetTrigger("Dead");
gameCanvas.SetTrigger("Hide");
//Check if this is a Highscore
if(score > PlayerPrefs.GetInt("Highscore"))
{
float s = score;
if (s % 1 == 0)
s += 1;
PlayerPrefs.SetInt("Highscore", (int)s);
}
}
public void RequestRevive()
{
ShowOptions so = new ShowOptions();
so.resultCallback = Revive;
Advertisement.Show("rewardedVideo",so);
}
public void Revive(ShowResult sr)
{
if(sr == ShowResult.Finished) // if ads was played succesfully
{
FindObjectOfType<PlayerMotor>().Revive();
IsDead = false;
foreach (GlacierSpawners gs in FindObjectsOfType<GlacierSpawners>())
gs.IsScrolling = true;
deathMenuAnim.SetTrigger("Alive");
gameCanvas.SetTrigger("Show");
}
else
{
OnPlayButton();
}
}
}
I recommend actually fleshing out your question. Where are you struggling? What unexpected behaviour are you getting?
FindObjectOfType().Revive();
this is the actual problem
And having this error
'Player$$anonymous$$otor' does not contain a definition for 'Revive' and no accessible extension method 'Revive' accepting a first argument of type 'Player$$anonymous$$otor' could be found (are you missing a using directive or an assembly reference?)
sorry I m a learner and found youtube tutorial very friendly so followed him and also created entire game m just stuck in this revive system. Thank you please help :)
Answer by laurG · Feb 28, 2019 at 04:56 AM
Can you be a bit more specific on what's not working?
Does the RequestRevive get called? (does the button for Revive work?)
Does the "if" in the Revive method return true? (the ShowResult is Finished)
If that's not the case, look into the how you get the ShowREsult, otherwise see what doesn't work properly from within the if (player/GlacierSpawners etc.) and debug that.
I suggest you use Debug.Log("some text you want"); within the methods that should be called to see if they are called properly. A more advanced approach at figuring out what's happening, is using the Debug Mode from Visual Studio (if you use it) (some info here).
Thank you so much for the reply, Actually, I Have player$$anonymous$$otor script from where I want to revive my player but unable to create that only lines script, glacier loop working fine.
FindObjectOfType().Revive(); this is the actual problem
And having this error
'Player$$anonymous$$otor' does not contain a definition for 'Revive' and no accessible extension method 'Revive' accepting a first argument of type 'Player$$anonymous$$otor' could be found (are you missing a using directive or an assembly reference?)
sorry I m a learner and found youtube tutorial very friendly so followed him and also created entire game m just stuck in this revive system. Thank you please help :)
Is he N3$$anonymous$$ with the penguing endless runner?
Yea, it seems that Player$$anonymous$$otor script is either missing or the dependency to it is not set properly.
Do you have the script in your Project view somewhere? (In the project view, search for Player$$anonymous$$otor in the search bar on top of it).
Let me know if it's there or not.
Was not able to put the entire code in reply to post it as answer hope you solve it. Thank you so much
Answer by azharm833 · Feb 28, 2019 at 05:51 AM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMotor : MonoBehaviour {
private const float LANE_DISTANCE = 2.5f;
private const float TURN_SPEED = 0.05f;
// Audio
public AudioClip playerHurt;
AudioSource playerAS;
//
private bool isRunning = false;
// Animation
private Animator anim;
// Movement private CharacterController controller; private float jumpForce = 4.0f; private float gravity = 12.0f; private float verticalVelocity; private int desiredLane = 1; // 0 = left, 1 = Middle, 2 = Right
// Speed Modifier
private float originalSpeed = 8.0f;
private float speed;
private float speedIncreaseLastTick;
private float speedIncreaseTime = 2.5f;
private float speedIncreaseAmount = 0.1f;
private void Start()
{
speed = originalSpeed;
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
playerAS = GetComponent<AudioSource>();
}
private void Update()
{
if(!isRunning)
return;
if(Time.time - speedIncreaseLastTick > speedIncreaseTime)
{
speedIncreaseLastTick = Time.time;
speed += speedIncreaseAmount;
GameManager.Instance.UpdateModifier(speed - originalSpeed);
}
// Gather the inputs on which lane we should be
if(MobileInput.Instance.SwipeLeft)
MoveLane(false);
if(MobileInput.Instance.SwipeRight)
MoveLane(true);
// Calculate where we should be in the future
Vector3 targetPosition = transform.position.z * Vector3.forward;
if (desiredLane == 0)
targetPosition += Vector3.left * LANE_DISTANCE;
else if (desiredLane == 2)
targetPosition += Vector3.right * LANE_DISTANCE;
// Let's calculate our move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
bool isGrounded = IsGrounded();
anim.SetBool("Grounded", isGrounded);
// Calculate Y
if(isGrounded) // if Grounded
{
verticalVelocity = -0.1f;
if(MobileInput.Instance.SwipeUp)
{
// Jump
anim.SetTrigger("Jump");
verticalVelocity = jumpForce;
}
else if(MobileInput.Instance.SwipeDown)
{
// Slide
StartSliding();
Invoke("StopSliding", 1.0f);
}
}
else
{
verticalVelocity -= (gravity * Time.deltaTime);
// Fast Falling mechanic
if(MobileInput.Instance.SwipeDown)
{
verticalVelocity = -jumpForce;
}
}
moveVector.y = verticalVelocity;
moveVector.z = speed;
// Move the Pengu
controller.Move(moveVector * Time.deltaTime);
// Rotate the pengu to where he is going
Vector3 dir = controller.velocity;
if (dir != Vector3.zero)
{
dir.y = 0;
transform.forward = Vector3.Lerp(transform.forward,dir, TURN_SPEED);
}
}
private void StartSliding()
{
anim.SetBool("Sliding", true);
controller.height /= 2;
controller.center = new Vector3(controller.center.x,controller.center.y /2, controller.center.z);
}
private void StopSliding()
{
anim.SetBool("Sliding", false);
controller.height *= 2;
controller.center = new Vector3(controller.center.x,controller.center.y *2, controller.center.z);
}
private void MoveLane(bool goingRight){
// Going Left
if(!goingRight)
{
desiredLane--;
if (desiredLane == -1)
desiredLane = 0;
}
else
{
desiredLane++;
if(desiredLane == 3)
desiredLane = 2;
}
}
private bool IsGrounded()
{
Ray groundRay = new Ray(
new Vector3(controller.bounds.center.x,(controller.bounds.center.y - controller.bounds.extents.y) + 0.2f,
controller.bounds.center.z),
Vector3.down);
Debug.DrawRay(groundRay.origin, groundRay.direction,Color.cyan,1.0f);
return Physics.Raycast(groundRay, 0.2f + 0.1f);
}
public void StartRunning()
{
isRunning = true;
anim.SetTrigger("StartRunning");
}
private void Crash()
{
anim.SetTrigger("Death");
isRunning = false;
playerAS.clip = playerHurt;
playerAS.PlayOneShot(playerHurt);
GameManager.Instance.OnDeath();
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
switch(hit.gameObject.tag)
{
case "Obstacle":
Crash();
break;
}
}
}
Your answer
Follow this Question
Related Questions
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer
I'm trying to allow my character to only jump when grounded 1 Answer
Point Counter Works Only Once! 1 Answer
Sprite dots spawn and have their own color, but they only show as wireframes? 0 Answers