So, I am making a rhythm game and I need to tell if the object, whose trigger I’m colliding, with has a variable of type float named note and what note’s value is. My idea was that it would be something like (* what I thought):
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == “Note” && *other.note == currentNote)
{
Destroy(other);
}
}
My entire code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class buttonDetector : MonoBehaviour
{
float currentNote;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
CheckButtonsDown();
CheckButtonsUp();
}
void CheckButtonsDown()
{
if(Input.GetKeyDown(KeyCode.RightArrow))
{
Debug.Log("right");
currentNote += 5;
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
Debug.Log("left");
currentNote += 50;
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("up");
currentNote += 500;
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("down");
currentNote += 5000;
}
}
void CheckButtonsUp()
{
if(Input.GetKeyUp(KeyCode.RightArrow))
{
Debug.Log("right off");
currentNote -= 5;
}
if(Input.GetKeyUp(KeyCode.LeftArrow))
{
Debug.Log("left off");
currentNote -= 50;
}
if(Input.GetKeyUp(KeyCode.UpArrow))
{
Debug.Log("up off");
currentNote -= 500;
}
if(Input.GetKeyUp(KeyCode.DownArrow))
{
Debug.Log("down off");
currentNote -= 5000;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Note" && other.note == currentNote)
{
Destroy(other);
}
}
}
and:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstacles : MonoBehaviour
{
public float speed = 1;
public float note;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float speedB = speed * Time.deltaTime;
transform.Translate(0, -speedB, 0);
}
}
Can anyone solve this, or is there a different way to handle this, keeping in mind I have several objects with the shorter script, and I need this to work individually on all of them.