Score system using prefabs in C#
I'm trying to make a score system where a prefabs cloner creates targets which you can't let be hit by a object that travels horizontally in the screen, if the prefabs is hit, a point is deducted for a certain amout of "lives". The problem is that i can't link this prefab the gameobject or the script used to manipulate the UI, nor could i make it use the variables on other scripts by using:
scoreScript scoreScript = GetComponent();
or
GameObject score = GameObject.FindWithTag("Score");
the two scripts (both the prefab and the score controller) right are like that:
the target's script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class loliScript : MonoBehaviour {
public int speed = 2;
private void Start(){
Rigidbody2D lolibody = GetComponent<Rigidbody2D>();
lolibody.velocity = new Vector2(0, -speed);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Car")
{
Destroy(this.gameObject);
}
}
void OnBecameInvisible() {
Destroy (gameObject);
}
}
and the Score Controller script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class scoreScript : MonoBehaviour {
public int score = 0;
public int record = 0;
public int lolis = 5;
public Text txScore;
public Text txRecord;
public Text txLolis;
void Update(){
txScore.text = "Score: " + score;
txRecord.text = "Record: " + record;
txLolis.text = "Lolis: " + lolis;
}
}
I've been trying to make this work for the last 6 hours, and every single tutorial i try to follow doesn't work. What exactly am i doing wrong?
Your answer
![](https://koobas.hobune.stream/wayback/20220612123813im_/https://answers.unity.com/themes/thub/images/avi.jpg)