G
G
GmDvlpr2020-01-07 14:23:07
JavaScript
GmDvlpr, 2020-01-07 14:23:07

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 5e1468818f2c9168898766.png
. 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
5e1469831aec8296566132.png
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);
        }
    }
    
}

and here with destruction
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);
      
    }
}

It seems to me that some kind of crutches work out for me, but as they say: "If it works, don't touch it,"
although I'm a beginner
asking for help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Proskurin, 2019-03-15
@dimastik1986

if so, how to determine if html or json came

Network tab in developer tools (explore developer tools - console, debug, network, profiling - this is the best frontender tool). Well, or according to the classics
, and look at the console what was displayed there.

G
GavriKos, 2020-01-07
@GavriKos

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

Are you trying to feed an object that does not belong to this prefab to the PREFABU?
1) Don't do that
2) If I were a unit, I wouldn't allow it to do that either
3) If you do it in the prefab editing mode OR NOT in the prefab instance already on the stage, then it shouldn't work like that.
About clicking:
OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
Is there a collider on the object on which the DELETE script hangs?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question