S
S
SkrinV2019-08-28 21:15:45
C++ / C#
SkrinV, 2019-08-28 21:15:45

How to make the destruction of two objects after the expiration of time on Unity?

The code on idea should request objects which on a touch with each other - are destroyed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AirUlt : MonoBehavior
{
public GameObject Ammo;
public GameObject Enemy;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter()
{
if (Trigger("Ammo") == Collision("Enemy" )) Destroy(GameObject(Ammo), 7f); } }
{
Destroy(GameObject(Enemy), 7f);
}
But in the end it gives me errors
Assets\Scripts\AirUlt.cs(22,13): error CS0103: The name 'Trigger' does not exist in the current context
Assets\Scripts\AirUlt.cs(22,32): error CS1955 : Non-invocable member 'Collision' cannot be used like a method.
Assets\Scripts\AirUlt.cs(24,21): error CS1955: Non-invocable member 'GameObject' cannot be used like a method.
Assets\Scripts\AirUlt.cs(25,21): error CS0103: The name 'GameObjcet' does not exist in the current context
How do I fix them?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
C
CHIDWI, 2019-09-01
@SkrinV

If it is fundamental to do just that, then you need to understand that there should be only one bullet in the scene, otherwise this object that "controls objects in the air" will not understand what to delete. Also, in order for the collision to "occur", there must be a collider on the objects. I would advise you to hang the script directly on a bullet or an enemy and already check the collision in them, which will be a plus in terms of the fact that you can, for example, spawn bullets from a prefab with a script. Then the code will be in the pool like this.

void OnCollisionEnter(Collision col){ 
  if (col.gameObject.name=="enemy") {
                Destroy (col.gameObject, 7f); //удаляем врага с !КОТОРЫМ! столкнулись.
  }
  	Destroy (gameObject, 7f); //удаляем нашу пулю если она в что либо врезалась.
}
void Update(){
        Destroy (gameObject, 10f); //удаляем нашу пулю если она никуда не врезалась и улетела в далёкие края.
}

D
dollar, 2019-08-29
@dollar

How to fix the problem

Fixing problems in a poorly performing program (game) is called debugging . The debugging process is a search, i.e. a sequence of checks and experiments, during which information is accumulated, which ultimately helps to guess where and what mistake was made.
The main method is the exception method . Approximately as in the game "Danetki" (or "Situations"), only in relation to the application ("yes" - it works, "no" - it does not work). Obviously, you need to choose the right "questions", then the answer will quickly be found. For example, you can try to execute the delete bullet function by itself without conditions. If it is executed, then it definitely works, and you can exclude it from further analysis. If it doesn't run, then it's the problem.into it, to the exclusion of everything else (for now). Thus, with just one experiment (question), the problem is divided in half, or rather, it is halved. Etc. For 10 such questions-checks, you will reduce the problem by 1000 times, and maybe already completely localize it by that time.
I know the answer is too general, but it really suits you. As useless and preachy as it sounds, my advice is to learn how to debug.
At the moment you have asked 0 questions so far, including this one. Why zero? You kind of asked the question, but the result was not described. You said what you need (for the enemy and the bullet to be removed), but did not say that at the moment there is. The enemy is removed, but the bullet is not? The bullet is removed, but the enemy is not? The game won't launch at all because of trying to make this feature? Does the bullet go right through? The bullet bounces off like concrete and hits the floor? Any of these tips would be the first step and help reduce the problem by a factor of 2 or even a factor of 10. But you didn't give that hint, so the problem could be anything. In this case, you can be helped either by telepathy (guessing), or by making your task from scratch, without taking into account your achievements.
But even if there is 1 clue, this will not be enough. As I said above, a few hints are needed. And where to look for the next one depends on where you found the previous one. So you and only you can do it. Through the Toaster, it will be in the format "try this - did it, it turned out so-and-so - then check it - ok, it gave out this - then fix it here", etc.
The code in this case is not very helpful, and I have already said why. Because the problem (which is not described) can be anywhere, even outside of this code.
Thanks for adding the error description to the question text. Now it is much clearer what is happening (these are already 4 hints), but it is still impossible to answer unequivocally. Because they are kind of obvious, their description speaks for itself, but, nevertheless, the question is still relevant. This indicates a lack of knowledge in C # or in English. I'll go through briefly:
  1. triggeryou are using but not declared anywhere. And the compiler complains that it doesn't know what it is. Perhaps you want to check something and it makes sense to you what it should be (some kind of "trigger"), but the compiler does not know English, for it it is only an identifier of some entity - whether it is a variable, whether a function, and etc. Also, even a human programmer will get stuck here because it's not clear what your Trigger("Ammo") refers to, nor is Collision("Enemy"). If you have a lot of enemies and bullets in the game, then what can this line mean? Collision of all with all? Or what? When (if) you understand the C# language and OOP in general, you will know that there are classes, objects and their methods. And that methods are usually called not by themselves, but in relation to some object, and at least to the current one (but there is no such method in the current one).
  2. Collision - here the compiler says that this identifier is familiar to it, but you are not using it as intended.
  3. GameObject - similar to the previous one, this is familiar to the compiler, but this is not possible, this is not a function.
  4. GameObjcet - and here the compiler says, as in the first post, that you didn't declare this identifier anywhere. Obviously it's just a typo. This is exactly the case when, while debugging , you have to guess about it. After all, GameObject is just the main word in Unity, but the compiler doesn't know it? What? How so? And if you exclude the impossible, then almost the only option remains - a typo. It's that simple.

D
Denis Gaydak, 2019-08-28
@MrMureno

and what is wrong?
like you know about collisions and triggers.
how to delete objects in a course.
about checking by tag - you also seem to know.
if this code is not a copy-paste, then describe what exactly does not work and what it swears at.
from what caught my eye
if (Enemi.tag == "UltA")
is that how the tag sees?? Enemi is a collision. you need to take a gemobject or a collider from her

S
SkrinV, 2019-08-31
@SkrinV

Sorry, the first versions of this post were very stupid

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question