I am trying to add a sound into my game that whenever the player moves over a certain space it plays a crunch sound. I have created the AudioSource file and a .OGG file for the sound.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpaceBlue : MonoBehaviour
{
public Transform spaceNext;
public AudioSource stepOnObject;
public AudioClip stepOnSound;
private void Start()
{
stepOnObject.clip = stepOnSound;
stepOnObject.enabled = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
stepOnObject.Play();
if (BoardScript.diceValue > 0)
{
BoardScript.diceValue -= 1;
Debug.Log("The dice are now " +BoardScript.diceValue);
other.transform.LookAt(spaceNext);
}
}
}
}
I have included the source and clip to my game object and i have tried it both with and without "Play on wake" selected.
Whenever the play walks over the player walks over the object i get a warning in the unity engine saying that the source is disabled.
Any help is appreciated :)
I had a similar problem and I fixed it by changing the location of the Play() call to after calling Destroy(GameObject). I would recommend trying moving the call to Play() to the end of the function, or trying Invoke("stepOnObject.Play", 0.5f); to ensure it gets called.
Otherwise, make sure its checkbox is ticked, and that the AudioSource actually has a AudioClip attached.
If you have any piece of code in some other script that Destroys this game object or makes SetActive false, then the best way to solve this problem will be to delay that piece of code by some time using a Coroutine.
Related
I'm working on a death script for my game in Unity. I made a 3d Box without textures underneath my level and made its Collider isTrigger = true. I now added a script to the box that reloads the current scene when the player enters the trigger. Its 2 lines of code and I don't know why but I get the error:
Assets\scripts\death.cs(20,32): error CS0103: The name 'currentScene' does not exist in the current context
The Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class death : MonoBehaviour
{
void Start()
{
Scene currentScene = SceneManager.GetActiveScene();
}
private void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(currentScene.buildIndex);
}
}
I know that the comments above already noticed your problem was the local variable and thanks to them, but this is just about to optimize your code and memory. you can just keep OnTriggerEnter only and delete Start.
private void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
there's no need to store the scene in a variable if we will not use in a further needs. it will be just a waste of memory (bad habit)
I am relatively new to Unity and have a recurring error. I am creating a simple 2D game in which the player can shoot these bullets at enemies, which are prefabs. In Unity itself, everything works fine but when I try to make a build in WebGL to play in my browser, and error keeps popping up saying my memory is out of bounds or index is out of range.
I have tried doing this on chrome, firefox, and explorer but the same thing keeps happening. I have tried searching the internet but have found no answers on how to fix this problem so far. Here is the code for my bullet script, if it helps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WebShooter : MonoBehaviour {
public Transform firePoint;
public GameObject bulletPrefab;
void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
Shoot();
}
}
void Shoot() {
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
private void OnTriggerEnter2D(Collider2D hitInfo) {
Destroy(gameObject);
}
}
https://imgur.com/G9ysLFM
Could it have something to do with the fact that you are not destroying the bullets that don't hit the gameobject? do you have any code to destroy them once they go off the screen?
If(Mathf.Abs(bulletPrefab.position) - Mathf.Abs(firePoint.position) > 10)
{
Destroy(gameObject);
}
I'm just thinking that you might be overloading the memory with lots of undestroyed bullets flying off into infinity
So I've been developing a 2D platformer game in C# and Unity and as a part of the game I have been developing powerups. One of these in invincibility, so when the player collides with the game object they cannot be killed for a period of time. I am relatively new to Unity and C# and read that I can use '.enabled' to enable/disable an external script that is attached to the same object. However, when I activate the powerup the object is destroyed but if I collide with an enemy or object I still die. Can anyone see why this is happening.
Below is the script that I have developed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InvincibilityPowerup : MonoBehaviour
{
public int Duration = 15;
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "Shield")
{
Destroy(GameObject.Find("Invincibility"));
StartCoroutine("Invincible");
}
}
IEnumerator Invincible()
{
Collision pIn = gameObject.GetComponent<Collision>();
pIn.enabled = false;
yield return new WaitForSeconds(Duration);
pIn.enabled = true;
}
}
1) GameObject.Find is completely unnecessary here. You already know which object is the invincibility powerup: its the one this script is attached to
2) Collision pIn = gameObject.GetComponent<Collision>(); both a) doesn't do what you want it to (you want to get the OTHER game object!) b) doesn't work anyway (Collision is not a component, Collider is)
3) you're destroying this before starting the coroutine, meaning your coroutine is being destroyed too.
I am using the FireComplex particle to generate an explosion (a bomb) when the user presses spacebar. The fire is playing on awake which I don't want and it doesn't seem to include an option to disable it.
Can this be coded to only activate the fire particle when I press space? I have tried the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CombatController : MonoBehaviour {
//public GameObject enemy;
[SerializeField]
public GameObject Bomb;
public GameObject Fire;
if(Input.GetKeyUp(KeyCode.Space)) {
BombExplosion ();
}
public void BombExplosion () {
//Create the Bombs Explosion Particle Effect.
Instantiate (Fire, this.gameObject.transform.position, Quaternion.identity);
Fire.Emit(5); //Burn for 5 seconds
}
Unity seems to be ignoring the Fire.Emit function.
The reason, I think, why the Bomb goes off after executing the game is because of the onviously incorrect logic in GetKeyUp. I doubt that it what you need.
Change it to GetKey instead.
Hope that clarifies the issue.
I'm trying to make a sound clip play when my player collides with a specific obstacle. I created an AudioSource on the obstacle, and wrote this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class sound : MonoBehaviour {
AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("Player"))
Debug.Log("hit");
GetComponent<AudioSource>().Play();
}
}
I gave the player a tag "Player", but when they collide, even though they both have a 2Dcollider component, the collision is not registered. There is no sound, nor does it say "hit" in the debug.log statement I wrote to check.
I have looked on the Unity documentation and I don't see what I'm doing wrong - what am I missing?
When working in a 2D environment with 2D component you need to use the 2D endings for some of your functions to work.
In your case you use
void OnCollisionEnter(Collision c)
But you need to use
void OnCollisionEnter2D(Collission2D c)
Docs:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html
Further more you might aswell use your audioSource.Play() since you already set the variable to the component in your Start(). Instead of GetComponent().Play();
If 3D,
void OnTriggerEnter(Collider other){
if(other.tag == "Player"){
this.GetComponent<AudioSource>().Play();
Debug.Log("PLayed Sound!");
}
}
Simple. Good Luck on whatever your doing!