I’ve got a custom class Pawn(GameObject _pawn, Field _field, char _type)
which instances are stored in a list.
Now using raycast I want to manipulate created Pawn objects. My problem is how to recognize which object is right now chosen? This is my code for now:
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f))
{
if(pawns[pawns.IndexOf(hit.transform.gameObject)].PawnType=='r')
Debug.Log("You selected the " + hit.transform.name); // ensure you picked right object
}
}
How to obtain Pawn id in my list? I’m trying to use IndexOf but it is expecting me Pawn object as an input and I’ve got parameter _pawn which is gameobject. I feel like I’m doing it wrong and there is completely different way to solve it, any advices?
You’re going to want your Pawn class to inherit from MonoBehaviour, and be attached to the pawn GameObject. The Pawn class does not need a reference to the GameObject, as MonoBehaviours have this by default.
Then you can do hit.transform.GetComponent() and perform checks or operations assuming that GetComponent did not return null.
Something like this (not a working tested example, just for reference):
public class Pawn : MonoBehaviour {
public int field;
public char type;
}
public class InputClass : MonoBehaviour {
void Update() {
if(Input.GetMouseButtonDown(0)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 100.0f)) {
Pawn pawn = hit.gameObject.GetComponent<Pawn>();
if(pawn != null) {
Debug.Log("You selected the pawn type: " + pawn.type);
}
}
}
}
}
Yeah, I thought about what you wrote but expected there is also some other way. I’m confused that I don’t need to use GameObject as a parameter.
I have an easier class called field in which I try to implement your idea. Here it is:
using UnityEngine;
public class Field : MonoBehaviour
{
private Color color;
private Vector3 pos;
public Color FieldColor
{
get { return color; }
set { color = value; }
}
public Field(Color _color, Vector3 _pos)
{
Instantiate(this.gameObject, _pos, Quaternion.identity);
this.color = _color;
this.pos = _pos;
SetColor(color);
}
public void SetColor(Color _color)
{
obj.GetComponent<Renderer>().material.color = _color;
}
}
I’m confused how to instiantiate it, because when I’m calling it like below it is not working. The script Field.cs is of course attached to prefab as you said. But when I’m calling the code below it is not creating the unity object only the class instance right?
Field newField = new Field(color1,new Vector3(i, 0, j));
Keep in mind a MonoBehaviour is a component. It is attached to a GameObject, and has a built-in ‘gameObject’ reference to it’s owning GameObject. You cannot create new MonoBehaviours with the keyword “new”. You must use the Instantiate function.
As such, you shouldn’t be using constructors for MonoBehaviours.
You don’t have to make it a MonoBehaviour if you’re holding a reference to the GameObject that represents the pawn in the world
foreach (var pawn in pawns)
{
// assuming PawnObject is the name of the property you're assigning _pawn to in your ctor
if (pawn.PawnObject == hit.transform.gameObject)
{
Debug.Log("You selected this pawn: " + pawn.type);
}
}