I have this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundPlayer : MonoBehaviour
{
public Transform Player;
public AudioSource SoundThatPlayed;
void OnTriggerEnter(Collider Player) => SoundThatPlayed.Play();
}
It works perfectly, but the sound plays when ANYTHING touches the part.
OnTriggerEnter fires when ANY collider enters the trigger. You can check for a specific tag, component, name, layer, etc. to filter out the unwanted triggers.
void OnTriggerEnter(Collider collider)
{
var playerScript = collider.GetComponent<Player>();
if (playerScript)
{
// An object with the Player script on it entered the trigger
//
}
var isPlayerByName = collider.name == "My Player Name";
var isPlayerByTag = collider.tag == "My Player Tag";
}
Related
I am making a script on an elevator that is supposed to teleport the player when they are next to it and press e
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class interactible : MonoBehaviour
{
public Transform player;
public Transform pos;
private bool collide;
void Start()
{
collide = false;
}
void OnCollisionEnter2D(Collision2D collider)
{
collide = true;
}
void OnCollisionExit2D(Collision2D collider)
{
collide = false;
}
void Update()
{
if (Input.GetKeyDown("e") && collide)
{
player.position = pos.position;
}
}
}
I have defined both player and pos and have made a 2d box collider for the elevator and set "is trigger" to true.
What am I doing wrong?
Your issue is that 2 triggers can't collide, only triggers and colliders can work with each other.
UPDATE: The main problem turned out to be that when you're dealing with triggers, you need to use OnTriggerEnter2D instead of OnCollisionEnter2D
Input.GetKeyDown accept enum so it should be
if(Input.GetKeyDown(KeyCode.E) && collide)
I would like my player to collide with an invisible collider and restart the scene right away but it seems like my script isn't working for some reason.
Added the tag 'Fall' on the invisible collider.
Added the script on the Invisible Collider Game Object.
Invisible collider object has Box Collider 2D Attached and is set ti: is Trigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class sceneRestart : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D platform)
{
if (platform.gameObject.tag == "Fall")
{
SceneManager.LoadScene("Main");
Debug.Log("Scene Restarted");
}
}
}
The script is attached to the invisible collider, so it needs to check if the colliding object is the player:
public class sceneRestart : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D Col)
{
if (Col.gameObject.tag == "Player")
{
SceneManager.LoadScene("Main");
Debug.Log("Scene Restarted");
}
}
I have followed a tutorial on Youtube (https://www.youtube.com/watch?v=_nRzoTzeyxU) on how to create a dialogue system for a game. Since my game is a platformer/RPG, I am currently attempting to adapt this system to where the player can walk up to an NPC and press the "Submit" button to access their dialogue, instead of clicking a button on the canvas/UI.
So far I have created an Interactable script that allows the player to detect if they are in the range of the invisible sphere collider that is equipped to the NPC, which is working. If I try to access the dialogueTrigger script that is equipped to the NPC however, I get a NullReferenceException error. I would like some help on how to properly call the dialogueTrigger script from the NPC and trigger the dialogue event, as I am very new to code and I only have this so far. Any help would be appreciated.
EDIT:
NullReferenceException: Object reference not set to an instance of an object
Interactable.Update () (at Assets/Scripts/Interactable.cs:34)
Dialogue Trigger is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue ()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
Interactable is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
private GameObject triggeringNpc;
private bool triggering;
public DialogueTrigger Diag;
void Start()
{
}
void Update()
{
if(triggering)
{
Debug.Log("Within Range");
if (Input.GetButtonDown("Submit"))
{
Debug.Log("Pressed the Interact Button");
Diag.TriggerDialogue();
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "NPC")
{
triggering = true;
triggeringNpc = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == "NPC")
{
triggering = false;
triggeringNpc = null;
}
}
}
The culprit would be your Diag reference. Have you correctly dragged a DialogueTrigger prefab from your Hierarchy window and into the public field on Interactable?
I am making a really short C# Unity game for a college class I am in and I have created a script for a trap that deactivates my player on contact that also includes a replay button. It all works except when I replay, the player remains inactive.
How would I modify my script to make it so the player reactivates on replay?
Also, this class I am in is a beginner class, I'm not super good at this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Trap : MonoBehaviour
{
public GameObject playerExplosion;
public GameObject gameOverUI;
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
other.gameObject.SetActive(false);
gameObject.SetActive(false);
gameOverUI.SetActive(true);
PlayerController.gameOver = false;
}
}
Edit: Here is the replay script too. It works on a health bar system.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ReplayGame : MonoBehaviour
{
public Transform player;
public Image uiBar;
public GameObject GameOverUI;
public static Vector3 startPosition;
private float fillAmount;
void Start()
{
startPosition = player.position;
fillAmount = uiBar.fillAmount;
GameOverUI.SetActive(false);
}
public void Click ()
{
PlayerController.gameOver = false;
player.position = startPosition;
uiBar.fillAmount = fillAmount;
GameOverUI.SetActive(false);
}
}
you will have to re Activate your player, or you have to Instantiate a new player.
public void Replay ()
{
//ReActivate
myPlayer.gameObject.SetActive(true);
//or Instnatiate a new
Instantiate(myPlayer);
}
I've been trying to make a sliding door for my unity level and I've managed to set up the animations but the scripting that's supposed to link up the functions with the objects isn't working.
Here's the script for the key card:
using UnityEngine;
using System.Collections;
public class Key_Pickup_1 : MonoBehaviour {
public GameObject player;
private Player_inventory playerinventory;
void Awake ()
{
playerinventory = player.GetComponent<Player_inventory>();
}
// Update is called once per frame
void onTriggerEnter()
{
if (gameObject == player)
{
playerinventory.hasKey_1 = true;
Destroy(gameObject);
}
}
}
Here's the script for the Door animation:
using UnityEngine;
using System.Collections;
public class Door_Animation_1 : MonoBehaviour {
public string Open;
private Animator anim_1;
public GameObject player;
private Player_inventory playerInventory;
void Start()
{
anim_1 = GetComponent<Animator>();
player = GameObject.FindGameObjectWithTag("Player");
playerInventory = player.GetComponent<Player_inventory>();
}
void OntriggerEnter (Collider other)
{
if(other.gameObject == player)
{
if (playerInventory.hasKey_1)
{
anim_1.SetTrigger(Open);
}
}
}
Any Ideas?
You don't have the proper capitalization for the OnTriggerEnter methods in your code. You have two different spellings and they are both wrong. It must be exactly OnTriggerEnter (or OnTriggerEnter2D for objects with a Collider2D instead of a Collider).