Answer the question
In order to leave comments, you need to log in
The text is not inserted into the text box of the unity object, what should I do?
I am creating a game in which an object appears and the player must click on it, after which the object is deleted and a point is added to the score. But then problems began, at first the unit did not destroy the instance, but the prefab itself, then I decided to simply create 2 scripts, in the first I wrote the spawn of the object, in the second - destruction, I tied the script 2 to the prefab itself, and here the 2nd problem begins - the text from the hierarchy does not want to be inserted into the text window
. But if I insert the script with the destruction into the camera or somewhere else, then everything is fine, but the joke is that if I put it on the camera, then nothing is pressed, and if I put it on the Controller, which I made from a 3d cube and removed the mesh renderer from there, then by clicking anywhere on the screen, the object will be deleted, but I need , so that the object is destroyed by clicking on the object itself, and not on any point.
I didn’t find this on the Internet, that’s how the
code with spawning objects works for me
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hitenemy : MonoBehaviour
{
public GameObject man;
GameObject enemy;
void Start()
{
StartCoroutine(Spawn());
}
void Update()
{
}
IEnumerator Spawn()
{
while (true)
{
man.transform.position = new Vector2(Random.Range(-2.21f, 2.195f), Random.Range(-4.354f, 2.498f));
enemy= Instantiate(man);
yield return new WaitForSeconds(1.5f);
// Destroy(enemy);
// yield return new WaitForSeconds(1.5f);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Destroyenem : MonoBehaviour
{
public Text score;
private int count = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnMouseDown()
{
count++;
score.text = count.ToString();
Destroy(gameObject);
}
}
Answer the question
In order to leave comments, you need to log in
if so, how to determine if html or json came
I attached the 2nd script to the prefab itself, and here the 2nd problem begins - the text from the hierarchy does not want to be inserted into the text window
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question