• 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 $$anonymous$$ · Jun 22, 2016 at 05:10 AM · c##pragma

how i can call the score , highscore and lifepoints

Hey Guys i'm working in a new game so the Score , highscore and lifepoints didnt be called how i can solve it ?

and i put it in my component, theres no errors !`score highscore lifepoints`

 Game Manger
 
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GameManager : MonoBehaviour
 {   
     public static GameManager Instance{ set; get; }
 
     private const float REQUIRED_SLICEFORCE = 400.0f;
 
     public GameObject vegetablePrefab;
     public Transform trail;
 
     private List<Vegetable> veggies = new List<Vegetable>();
     private float lastSpawn;
     private float deltaSpawn = 1.0f;
     private Vector3 lastMousePos;
     private Collider2D[] veggiesCols;
 
     // UI part of the game thingy
     private int score;
     private int highscore;
     private int lifepoint;
     public Text scoreText;
     public Text highscoreText;
     public Image[] lifepoints; 
 
     private void Awake()
     {
         Instance = this;
     }
 
     private void Start()
     {
         veggiesCols = new Collider2D[0];
         NewGame();
     }
 
     private void NewGame()
     {
         score = 0;
         lifepoint = 0;
     }
 
     private void Update()
     {
         if (Time.time - lastSpawn > deltaSpawn)
         {
             Vegetable v = GetVegetable();
             float randomX = Random.Range(-1.65f, 1.65f);
 
             v.LaunchVegetable(Random.Range(1.85f, 2.75f), randomX, -randomX);
             lastSpawn = Time.time;
         }
         if (Input.GetMouseButton(0))
         {
             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             pos.z = -1;
             trail.position = pos;
 
             Collider2D[] thisFramesVeggies = Physics2D.OverlapPointAll(new Vector2(pos.x, pos.y), LayerMask.GetMask("vegetable"));
             if ((Input.mousePosition - lastMousePos).sqrMagnitude > REQUIRED_SLICEFORCE)
             {
                 foreach (Collider2D c2 in thisFramesVeggies)
                 {
                     for (int i = 0; i < veggiesCols.Length; i++)
                     {
                         if (c2 == veggiesCols[i])
                         {
                             c2.GetComponent<Vegetable>().Slice();
                         }
                     }
                 }
                 lastMousePos = Input.mousePosition;
                 veggiesCols = thisFramesVeggies;
             }
         }
     }
 
     private Vegetable GetVegetable()
     {
         Vegetable v = veggies.Find(x => !x.IsActive);
 
         if(v == null)
         {
             v = Instantiate(vegetablePrefab).GetComponent<Vegetable>();
             veggies.Add(v);
         }
         return v;
     }
 
     public void IncrementScore(int scoreAmount)
     {
         score += scoreAmount;
         scoreText.text = score.ToString();
 
         if (score > highscore)
         {
             highscore = score;
             highscoreText.text = "BEST:" + highscore.ToString();
         }
     }
 
     public void LoseLP()
     {
         if (lifepoint == 0)
             return;
 
         lifepoint--;
         lifepoints[lifepoint].enabled = false;
         if (lifepoint == 0)
             Death();
     }
 
     public void Death()
     {
         Debug.Log("Death");
     }
 }

 Main C#
 
 using UnityEngine;
 using System.Collections;
 
 public class Vegetable : MonoBehaviour
 {
     private const float GRAVITY = 2.0f;
 
     public bool IsActive { set; get; }
 
     private float verticalVelocity;
     private float speed;
     private bool isSliced = false;
 
 
     public void LaunchVegetable(float verticalVelocity, float xSpeed, float xStart)
     {
         IsActive = true;
         speed = xSpeed;
         this.verticalVelocity = verticalVelocity;
         transform.position = new Vector3(xStart, 0, 0);
         isSliced = false;
     }
 
     private void Update()
     {
         if (!IsActive)
             return;
 
         verticalVelocity -= GRAVITY * Time.deltaTime;
         transform.position += new Vector3(speed, verticalVelocity, 0) * Time.deltaTime;
 
         if (transform.position.y < -1)
         {
             IsActive = false;
             if (!isSliced)
                 GameManager.Instance.LoseLP();
         }
     }
 
     public void Slice()
     {
         if (isSliced)
             return;
 
         if (verticalVelocity < 0.5f)
             verticalVelocity = 0.5f;
 
         speed = speed * 0.5f;
         isSliced = true;
 
         GameManager.Instance.IncrementScore(1);
     }
 }
 

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 Mindmapfreak · Jun 24, 2016 at 12:34 AM 0
Share

I think you have to elaborate on your problem. What do you want to call? I do not see a function Score(), Highscore() or Lifepoints() ... Do you want to show the score in the UI? Something like this.scoreText.text=this.score.ToString(); would do that (assu$$anonymous$$g you set up the scoreText correctly).

avatar image $$anonymous$$ Mindmapfreak · Jun 29, 2016 at 09:31 PM 0
Share

I want too call the score , high score and life points and i add them in the UI Sorry for late

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Cannot Send Class Object From One Scene to Another 1 Answer


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