cannot convert type HealthandInventory to int
Hey guys,
this is a project I am working on and I am getting this error in Unity - Cannot implicitly convert type HealthandInventory to type int - HealthandInventory is the name of one of the scripts I have created, this error I am getting is in my PitTrigger Script. Any Ideas? Error says it is on line 25 in the 3rd nested IF Statement. Here's the code.
using UnityEngine;
using System.Collections;
public class PitTrigger : MonoBehaviour {
public int _playerlives;
void Start ()
{
_playerlives = 3;
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
{
GameObject trigger = GetNearestCP ();
if (trigger != null)
{
if (other.GetComponent<HealthandInventory>()._playerLives > 0)
{
other.transform.position = trigger.transform.position;
other.GetComponent<HealthandInventory>()._playerLives = other.GetComponent<HealthandInventory>();
}
else
{
Application.LoadLevel (0);
}
}
else
{
Debug.Log ("No Checkpoint Found");
}
}
else
{
Destroy (other.gameObject);
}
}
GameObject GetNearestCP ()
{
GameObject[] checkpoints = GameObject.FindGameObjectsWithTag ("Checkpoint");
GameObject nearestCheckpoint = null;
float shortestDistance = Mathf.Infinity;
foreach (GameObject checkpoint in checkpoints)
{
Vector3 checkpointPosition = checkpoint.transform.position;
float distance = (checkpointPosition - transform.position).sqrMagnitude;
CheckpointTrigger triggerRef = checkpoint.GetComponent<CheckpointTrigger>();
if (distance < shortestDistance && triggerRef.isCpTriggered == true)
{
nearestCheckpoint = checkpoint;
shortestDistance = distance;
}
}
return nearestCheckpoint;
}
}
not sure what you are trying to achieve on line 25, bt as the error suggests, you cannot convert and int to a component. _playerLives I assume is an int and HealthandInventory a component, hence :
other.GetComponent<HealthandInventory>()._playerLives = other.GetComponent<HealthandInventory>();
will throw an error. Perhaps you meant to write :
other.GetComponent<HealthandInventory>()._playerLives = _playerLives;
or :
_playerLives = other.GetComponent<HealthandInventory>()._playerLives ;
this is for a school project, this is the code that I got from my professor during class, which is why I am confused on the error. Ill give your code a try and see if it works. Thanks
Your answer
Follow this Question
Related Questions
How to setactive Car Script on writing script on Java on Android platform 0 Answers
NetworkIdentity.netID to int 3 Answers
Timespan to int total seconds 0 Answers
Cannot implicitly convert type `string' to `int' 0 Answers
Particle System trigger script not working,Particle system trigger script not working 0 Answers