I previously made a code for collectible items and tried doing the same thing here but for some reason, in inspector, I cannot assign a text mesh pro or anything to the public TextMeshProUGUI or the winTextObject even though I could in the collectibles code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class destroyOnContactBlank : MonoBehaviour
{
//declare our list of strings we'll use if we want our projectile to destroy an object it hits
public List<string> destroyableObjects = new List<string>();
public GameObject explosionEffect;
public TextMeshProUGUI countText;
public GameObject winTextObject;
private int count;
void Start()
{
// Set the count to zero
count = 0;
SetCountText();
// Set the text property of the Win Text UI to an empty string, making the 'You Win' (game over message) blank
winTextObject.SetActive(false);
}
private void OnCollisionEnter(Collision collision)
{
//run through our list of tags we want to destroy
for (int i = 0; i < destroyableObjects.Count; i++)
{
//check if the hit object has one of our tags
if(collision.gameObject.tag == destroyableObjects[i])
{
//destroy the hit object and the projectile
// Add one to the score variable 'count'
count = count + 1;
// Run the 'SetCountText()' function (see below)
SetCountText();
Destroy(gameObject);
Destroy(collision.gameObject);
GameObject explosion = Instantiate(explosionEffect, transform.position, transform.rotation);
Destroy(explosion, 5);
}
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 12)
{
// Set the text value of your 'winText'
winTextObject.SetActive(true);
}
}
}
This is the collectibles code and it works fine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
// Create public variables for player speed, and for the Text UI game objects
public class PlayerController : MonoBehaviour
{
// Create public variables for player speed, and for the Text UI game objects
public float speed;
public TextMeshProUGUI countText;
public GameObject winTextObject;
public GameObject ball;
private float movementX;
private float movementY;
private Rigidbody rb;
private int count;
// At the start of the game..
void Start()
{
// Assign the Rigidbody component to our private rb variable
rb = GetComponent<Rigidbody>();
// Set the count to zero
count = 0;
SetCountText();
// Set the text property of the Win Text UI to an empty string, making the 'You Win' (game over message) blank
winTextObject.SetActive(false);
}
private void Update()
{
if (transform.position.y < -5)
{
ball.transform.position = Vector3.zero;
}
}
void FixedUpdate()
{
// Create a Vector3 variable, and assign X and Z to feature the horizontal and vertical float variables above
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
// ..and if the GameObject you intersect has the tag 'Pick Up' assigned to it..
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
// Add one to the score variable 'count'
count = count + 1;
// Run the 'SetCountText()' function (see below)
SetCountText();
}
else if (other.gameObject.CompareTag("Gate") && count >= 12)
{
other.gameObject.SetActive(false);
}
}
void OnMove(InputValue value)
{
Vector2 v = value.Get<Vector2>();
movementX = v.x;
movementY = v.y;
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 12)
{
// Set the text value of your 'winText'
winTextObject.SetActive(true);
}
}
}
But they show up in the inspector and everything else looks normal? If that’s the case, then you’re probably trying to create an invalid connection like dragging a scene reference into a slot on a prefab. Post some screenshots of your inspector and project.
Yeah, that seems to be the issue. I was trying to drag text in the science onto a prefab for a projectile that destroys enemies. I’ll have to find another way to track destroyed enemies. Thank you for your clarifcation.
“cant add text(tmp) to UnityEvent”
I tried to do this for Prefab too! Thanks for your reply <3