Hi everyone! First, my code, then my problem:
RaycastHit hit;
if (Physics.Raycast (new Vector3 (xPos, 0f, zPos), Vector3.down, out hit, 3)) {
if (hit.transform.gameObject.CompareTag ("Case")){
//OnCase = hit.Case.CaseNumber;
}
}
So you see the line after the “//”, is something wrong. But this is the idea of what i want: i want to get the “CaseNumber” of the “Case” component (that i created myself, that is actually a c# script) that is on the hit object. How can i do that?
Thank you, Thomas.
Case theCase = hit.transform.GetComponent<Case>();
// Then do whatever you want with the Case.
OR
Case theCase = hit.collider.GetComponent<Case>();
// Then do whatever you want with the Case.
So basically, this should work? Because it doesn’t, but it could be anything else, and i don’t know since it’s not doing any compiling errors:
if (hit.transform.gameObject.CompareTag ("Case")){
Case theCase = hit.transform.GetComponent<Case>();
OnCase = theCase.CaseNumber;
}
That will work, yes, assuming everything else in the project/scene is set up properly.
Now it’s time to start debugging! Sprinkle your code with log statements like so:
Debug.Log("Sending out raycast!");
RaycastHit hit;
if (Physics.Raycast (new Vector3 (xPos, 0f, zPos), Vector3.down, out hit, 3)) {
Debug.Log("Raycast hit some object called " + hit.transform.name);
if (hit.transform.gameObject.CompareTag ("Case")){
Debug.Log("The object had the Case tag");
Case theCase = hit.transform.GetComponent<Case>();
if (theCase != null) {
Debug.Log("The object had a Case component attached");
}
}
}
Then run it and see what gets printed out and, more importantly, what doesn’t get printed out!
And after a solid 15 minutes of debugging, i found! There were a little error in the coordinates, it all works fine now! Thank you!
1 Like