I’m making a game where you have to hit game objects, the problem is that no code is working for me…
I just want to destroy and play sound, does somebody know how do this?
This is my code for destroying a game object and for the audio I don’t know how to do that:
I saw you posting similar questions earlier… the things you have asked are (mostly) already explained in introductory tutorial videos and in manual or in API reference.
“the problem is that no code is working for me…”
You show barely anything and practically ask others to fill in the part you haven’t even started yet. I see no audio related code, so did you google for this?
If you had googled for this issue, you would know, that you will get solutions from Unity Answers, YouTube videos and from manual / API page links.
Just put Unity + your question to google and you’ll see.
It is not that I don’t want to help, but you should learn to help yourself if you want to get something done with code. There is no way around needing to search for all kinds of solutions, you’ll have to do that all the time.
You already know the steps, just google each step and combine those. Then see if you still can’t get it done.
since all gameobjects destroying automatically before loading new scene
for me better work SetActive (true) and SetActive (false)
public GameObject yourscenegameobject;
void Update() {
//for better compatibility with others platforms
#if UNITY_ANDROID
if ()
{
yourscenegameobject.SetActive (true);
}
#endif
else
{
yourscenegameobject.SetActive (false);
}
}
sure if you you have a big game project with a lot of gameobjects on the scene where that`s really needed
try doing like this
void destroygameobjectonyourscene()
{
gameObject.tag = "tag your gameobject";
//destroying gameobject after 5 second
Destroy(gameObject, 5);
}
void OnCollisionEnter2D(Collision2D other)
{
#if UNITY_ANDROID
if (other.gameObject.tag == "tag your gameobject")
{
destroygameobjectonyourscene();
}
else {
}
}
If you have some problems with destroying object on the scene and always see messages on console
can`t destroy gameobject because this gameobject allready destroying or something else like this,
just enable or disable your gameobjects on the scene through SetActive (true) and SetActive (false)
Well if you want to destroy your object, then do so after the sound has played. You can delay the Destroy() like this:
audioSource.PlayOneShot("YourClipHere"); // Assuming your clip is 0.5 seconds long
Destroy(gameObject, 0.6f); // Delay destroy 0.6th of a second
Now you can access your game objects sprite renderer and collider and set its activeness to false so it looks destroyed to the player.
audioSource.PlayOneShot("YourClipHere"); // Assuming your clip is 0.5 seconds long
gameObject.GetComponent<SpriteRenderer>().enabled = false;
gameObject.GetComponent<Collider2D>().enabled = false;
Destroy(gameObject, 0.6f); // Delay destroy 0.6th of a second
One last method worth mentioning is:
PlayClipAtPoint('YourClipHere", gameObject.transform.position, 1.0f);
// Enter your audioclip, Vector3 coordinates,and volume
// This creates an audioSource at that location, plays the clip,
// and you can custom set the volume. Pretty cool, right? :)
The Audio Source is destroyed when the clip finishes.
using UnityEngine;
using System.Collections;
public class RedEnemy : MonoBehaviour
{
void Start()
{
AudioSource.PlayOneShot("explosion"); // Assuming your clip is 0.5 seconds long
gameObject.GetComponent<SpriteRenderer>().enabled = false;
gameObject.GetComponent<Collider2D>().enabled = false;
Destroy(gameObject, 0.6f); // Delay destroy 0.6th of a second
}
void Update ()
{
}
}
Is the script provided above your RedEnemy Script? If so, then what is on line 11? That’s where you’re getting your error from but I don’t see anything there.
Please do not post answers like these cus I am also looking for a solution for a similar problem and it is annoying to see such answers since they are not helpful but more like scolding. Either write an actual helpful answer or dont write anything. I dont think you can teach a life lesson to a stranger on a unity forum.