Unassigned Reference Exception although it is assigned
I'm having issues with assigning a GameObject to my script. I've tried assigning it through the inspector and defining it in code, but neither worked. I started to get the error when I introduced touch inputs into the game. But I can't seem to fix it now. Any help would be much appreciated!
public class HeartTap : MonoBehaviour
{
public GameObject heart;
public Vector2 touchPosition, heartPosition;
void Start()
{
}
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
touchPosition = new Vector2(Camera.main.ScreenToWorldPoint(touch.position).x, Camera.main.ScreenToWorldPoint(touch.position).y);
heartPosition = new Vector2(heart.transform.position.x, heart.transform.position.y);
print("X Touch: " + touchPosition.x + " Y Touch: " + touchPosition.y);
print("X Heart: " + heartPosition.x + " Y Heart: " + heartPosition.y);
if (touchPosition == heartPosition)
{
print("Good");
}
}
}
You most likely have another gameObject with the sale script and you forgot to assign the heart
gameObject.
Type t:HeartTap
in the search bar of the Scene view to find the gameObjects with the HeartTap
script.
Your answer
Follow this Question
Related Questions
8-way 2D top down movement (diagonal) idle animation issues 0 Answers
how can i touch the fast objects ? 1 Answer
How to translate Input axis into directions (in a 2D environment)? 0 Answers
Function runs twice 0 Answers
New Input System: ArgumentException: Event must be a state or delta state event 3 Answers