- Home /
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
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).
I want too call the score , high score and life points and i add them in the UI Sorry for late