- Home /
Console prints message multiple times
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);
}
}
Comment
Answer by FlaSh-G · Jul 13, 2018 at 10:05 PM
If you are sure that you clicked only once, the only reason to have this message printed multiple times is that you have this script component in your scene multiple times, and each instance of it prints once.
Your answer
Follow this Question
Related Questions
Render tooltip on top of everything with MRTK 0 Answers
Child instantiated object to RayCastHit's parent. 1 Answer
Layer Mask Detection 2 Answers