I am working on a small Unity project and am relatively new. I am trying to have multiple objects be able to be selected by a click using raycasting. The objects are a prefab and have both circle collider2ds and rigidbody2ds. When There is one object on the field the code works fine. It detects that the object is there and "Selects" it. But when I try to run the same code with two objects it doesn't detect the colliders for either of the objects. I assume it is an issue with the editor and not the code but there is probably a better way of achieving what I want to have happen. Thanks for any help!
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class CharacterMovment : MonoBehaviour
{
GameObject selected = null;
Characteristics characteristics;
public GameObject screenText;
private TMP_Text textComponent;
bool characterSelected = false;
private void Awake()
{
textComponent = screenText.GetComponent<TMP_Text>();
}
// Update is called once per frame
void Update()
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (Input.GetMouseButtonDown(0)){
Debug.Log(hit.collider.gameObject.tag);
if (hit.collider.gameObject.tag == "Character") {
characterSelected = true;
selected = hit.collider.gameObject;
characteristics = selected.GetComponent<Characteristics>();
Debug.Log("Adding character to selected. Character name: " characteristics.name);
}else{
characterSelected = false;
}
}
if (characterSelected) {
textComponent.text = "Selected Character: " characteristics.name;
}
}
}```
CodePudding user response:
If you want to get all objects in the mouse position , you can use RaycastAll. Please clarify if I misunderstood.
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class CharacterMovment : MonoBehaviour
{
GameObject selected = null;
List <Characteristics> characteristics;
public GameObject screenText;
private TMP_Text textComponent;
bool characterSelected = false;
private void Awake()
{
textComponent = screenText.GetComponent<TMP_Text>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D[] hit = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
for (int i = 0; i < hit.Length; i )
{
Debug.Log(hit[i].collider.gameObject.tag);
if (hit[i].collider.gameObject.tag == "Character")
{
characterSelected = true;
selected = hit[i].collider.gameObject;
characteristics.Add(selected.GetComponent<Characteristics>());
Debug.Log("Adding character to selected. Character name: " characteristics.name);
}
else
{
characterSelected = false;
}
if (characterSelected)
{
textComponent.text = "Selected Character: " characteristics[i].name;
}
}
}
}
}
CodePudding user response:
When you click script runs on every character. So basically you need only one instance of the MonoBehaviour for registering clicks. Right now you have CharacterMovement script on each character and it runs on each character multiple times when you click. Try to move logic to separate single script.