I have used getcomponent many times before but this is my first time trying to do it in 2d (shouldnt make a difference right?) I am instancing an object and trying to get a variable from another script.
first script (on the instanced object):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class moveLeft : MonoBehaviour
{
public float speed;
void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("we hit something");
if (collision.gameObject.tag == gameObject.tag)
{
GameObject.FindGameObjectWithTag("UI").GetComponent<score>().playerScore += 1;
Destroy(collision.gameObject);
Destroy(gameObject);
}
else if (collision.gameObject.tag != gameObject.tag)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
second script (on text handler):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class score : MonoBehaviour
{
public int playerScore;
public Text scoreText;
private void Update()
{
scoreText.text = "Score: " + playerScore.ToString();
}
}
if you could help thank you so much!