Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by azharm833 · Feb 28, 2019 at 04:18 AM · unity 5scripting problemscripting beginner

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();
     }

 }
 

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Randomman159 · Feb 28, 2019 at 04:47 AM 0
Share

I recommend actually fleshing out your question. Where are you struggling? What unexpected behaviour are you getting?

avatar image azharm833 Randomman159 · Feb 28, 2019 at 12:32 PM 0
Share

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 :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image azharm833 · Feb 28, 2019 at 05:26 AM 0
Share

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 :)

avatar image laurG azharm833 · Feb 28, 2019 at 05:36 AM 0
Share

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.

avatar image azharm833 laurG · Feb 28, 2019 at 05:53 AM 0
Share

Was not able to put the entire code in reply to post it as answer hope you solve it. Thank you so much

Show more comments
avatar image
0

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;
     }


 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

262 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer

How can i count the number of times i look to the transform.rotation.y < -0.2,How to count the amount of times i look down? 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges