Hi!
I`ve made simple game and have a problem with visualization of lives left. I use GameObject called LivesCounter whitch is children of Main Camera to show sprites with 3,2 or 1 hearth. I have 3 sprites in 3 clips and transitions set to trigger when int lives has correct value
(ThreeLives clipe change to TwoLives when int lives = 2, TwoLives change to OneLive when int lives = 1). The problem is that i cant get int lives value from Player GameObject to LivesCounter.
This is script attached to GetLives:
using UnityEngine;
using System.Collections;
public class GetLives : MonoBehaviour {
const int lives = 3;
Lives script;
void Start () {
script = GameObject.Find ("Player").GetComponent<Lives>();
}
void Update () {
Debug.Log ("Lives left" + lives);
}
}
and this is main script of Player GameObject
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float turnSpeed;
private Vector3 moveDirection;
private List<Transform> congaLine = new List<Transform> ();
private bool isInvicible = false;
private float timeSpentInvicible;
public int lives;
public AudioClip enemyContactSound;
public AudioClip kidContactSound;
[SerializeField]
private PolygonCollider2D[] colliders;
private int currentColliderIndex = 0;
// Use this for initialization
void Start () {
lives = 3;
moveDirection = Vector3.right;
}
// Update is called once per frame
void Update () {
Vector3 currentPosition = transform.position;
if (Input.GetButton ("Fire1")) {
Vector3 moveToward = Camera.main.ScreenToWorldPoint ( Input.mousePosition );
moveDirection = moveToward - currentPosition;
moveDirection.z = 0;
moveDirection.Normalize ();
}
Vector3 target = moveDirection * moveSpeed + currentPosition;
transform.position = Vector3.Lerp ( currentPosition, target, Time.deltaTime );
float targetAngle = Mathf.Atan2 (moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation =
Quaternion.Slerp (transform.rotation,
Quaternion.Euler ( 0, 0, targetAngle ),
turnSpeed * Time.deltaTime);
EnforceBounds ();
if (isInvicible)
{
timeSpentInvicible += Time.deltaTime;
if (timeSpentInvicible < 3f) {
float remainder = timeSpentInvicible % .3f;
renderer.enabled = remainder > .15f;
}
else {
renderer.enabled = true;
isInvicible = false;
}
}
}
public void SetColliderForSprite( int spriteNum )
{
colliders [currentColliderIndex].enabled = false;
currentColliderIndex = spriteNum;
colliders [currentColliderIndex].enabled = true;
}
void OnTriggerEnter2D( Collider2D other )
{
if (other.CompareTag ("kid")) {
Transform followTarget = congaLine.Count == 0 ? transform : congaLine[congaLine.Count-1];
other.transform.parent.GetComponent<kidController>().JoinConga( followTarget, moveSpeed, turnSpeed );
congaLine.Add ( other.transform );
audio.PlayOneShot(kidContactSound);
if (congaLine.Count >= 10) {
Debug.Log("You won!");
Application.LoadLevel("YouWin");
}
}
else if(!isInvicible && other.CompareTag ("enemy")) {
isInvicible = true;
timeSpentInvicible = 0;
audio.PlayOneShot(enemyContactSound);
for( int i = 0; i < 2 && congaLine.Count > 0; i++ )
{
int lastIdx = congaLine.Count-1;
Transform kaczuszka = congaLine[ lastIdx ];
congaLine.RemoveAt(lastIdx);
kaczuszka.parent.GetComponent<kidController>().ExitConga();
}
if (--lives <= 0) {
Debug.Log("You lost!");
Application.LoadLevel("YouLoose");
}
}
}
private void EnforceBounds()
{
Vector3 newPosition = transform.position;
Camera mainCamera = Camera.main;
Vector3 cameraPosition = mainCamera.transform.position;
float xDist = mainCamera.aspect * mainCamera.orthographicSize;
float xMax = cameraPosition.x + xDist;
float xMin = cameraPosition.x - xDist;
if (newPosition.x < xMin || newPosition.x > xMax) {
newPosition.x = Mathf.Clamp (newPosition.x, xMin, xMax);
moveDirection.x = -moveDirection.x;
}
float yMax = mainCamera.orthographicSize;
if (newPosition.y < -yMax || newPosition.y > yMax) {
newPosition.y = Mathf.Clamp (newPosition.y, -yMax, yMax);
moveDirection.y = -moveDirection.y;
}
transform.position = newPosition;
}
public int ReporLives()
{
return lives;
}
}
I also made helper script that is inside Player GameObject as a second script.
using UnityEngine;
using System.Collections;
public class Helper : MonoBehaviour
{
public Lives script;
int hasLives;
void Start(){
script = GetComponent<Lives>();
}
void Update() {
hasLives = script.ReportLives();
}
}
This should work but i see 2 errors in Unity:
First is in GetLives.cs
Assets/GetLives.cs(6,9): error CS0246: The type or namespace name `Lives’ could not be found. Are you missing a using directive or an assembly reference?
Second in Helper.cs
Assets/Helper.cs(6,16): error CS0246: The type or namespace name `lives’ could not be found. Are you missing a using directive or an assembly reference?
I realy dont have a clue what i did wrong.
Any suggestions?
I will be glad if You will help me.
Cheers!