Hey!
I want to make a Select/DeselectAll script. The console prints out the correct text but the problem is that I really can’t solve why it prints it out multiple times?
Thanks!
Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelectSingleObject : MonoBehaviour
{
//Private varibles
private int _layerGround;
private RaycastHit _hit;
private void Start()
{
_layerGround = LayerMask.NameToLayer("Ground");
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out _hit, Mathf.Infinity))
if (_hit.transform.gameObject.layer == _layerGround)
{
PrintName("Touched the ground");
}
else
{
PrintName("Touched an object");
}
}
}
private void PrintName(string _action)
{
print(_action);
}
}