Question by
Komsur · Jan 26, 2018 at 04:21 PM ·
c#raycastingdebug
Why is my logic happening 9 times?
Hi, so I'm trying to create a keypad with 9 buttons that represent 1 - 9, and I've set up a Raycast that allows me to select and indeed click the buttons, which Unity recognises. Problem is, it seems to be passing through the same logic, as if I have pressed all 9 buttons at once, despite it registering me clicking only one. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Selectable : MonoBehaviour
{
public CharacterController player;
public GameObject Raycaster;
public SelectionZoneController zone;
protected void Start()
{
player.addSelectable(this);
}
public void Update()
{
if (zone.inside)
{
if (GameObject.Find("Character/MainCamera/Raycast").GetComponent<RaycastForward>().hitSomething)
{
if (Input.GetMouseButtonDown(0) && player.hovered != "")
{
player.selected = player.hovered;
}
else if (Input.GetMouseButtonDown(1) && player.hovered != "")
{
player.selected = "";
}
}
}
else if (!zone.inside && gameObject.tag == player.selected)
{
player.selected = "";
}
}
}
public class Climbing : MonoBehaviour
{
public CharacterController player;
protected void Start()
{
player.addladder(this);
}
}
public class Keypad : MonoBehaviour
{
public CharacterController player;
public GameObject Raycaster;
public SelectionZoneController zone;
protected void Start()
{
player.addKeypad(this);
}
public void Update()
{
if (zone.inside)
{
if (GameObject.Find("Character/MainCamera/Raycast").GetComponent<RaycastForward>().hitSomething)
{
if (Input.GetMouseButtonDown(0) && player.hovered != "")
{
player.pressed = player.hovered;
{
Debug.Log("I am number 1");
}
}
if (Input.GetMouseButtonUp(0))
{
player.pressed = "";
}
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612151334im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there any way how to do raycasts like this? 0 Answers
Raycast does not seem to properly detect object it hit 0 Answers
Unity VR WebSocket Connection to ThingsBoard Error 'Failed to parse websocket command!' 1 Answer
Error CS0103: The name "Debug" does not exist in the current context? 0 Answers
How to change direction of RayCast based on camera angle? 0 Answers