If I raycast an object and the hit info gives me the name of the object. Lets say “cube”. How can I save this string??? saveName =hit.transform.name
But If the raycast hits an other Object lets say Sphere the code above will be updated. And the saveName is named as sphere.
But I want to save the string of the cube and if the raycast hits another obect I want to compare if(saveName !=hit.transform.name) then do…
I can not use Tags vbecause Im checkingt the triangle index`s of an object. and therefore I must know when the Index is chancing.
Thanks for links and advices…
Hi
I think you can do like
all of the cubes should be on same layers lets say ‘RayCast’.
& other gameobjects will be on another later say ‘IgnoreRaycast’ now you will check before saving the name that the hitted object is on raycast layer or not…
if it is on ‘Raycast’ only then it will save name
Example: Unity - Manual: Layers
Your question is quite vague but from what i understand you could just store the name to a variable and compare it the next time you get a hit.
private string lastHitObjectName;
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.name == lastHitObjectName) {
// you hit an object with the same name as last time
}
// store the objects name so we can compare
// it on the next hit
lastHitObjectName = coll.gameObject.name;
}
I better specify my problem.
I have a camera with a Raycast transDirection.fwd. and hits a sphere. (works only with a mesh collider attached to a primitive) And this raycast gives me a triangle index back. When I move the camera the Triangle index is changing, if the ray hits another Triangle of the mesh.
What I need, is to print out “The triangle Index has changed”
My problem is that, if I store like this: var saveTriIndex = Hit.triangleIndex; The variable itself changes too. If the raycast hits a new triangle.
So I cannot compare, because the variable already has changed trough the update. So I need the old TriangleIndex. Something like deltaTriangleIndex…
function Update () {
Debug.DrawRay(transform.position,transform.TransformDirection(0,0,1)*1000,Color.red);
var hhh :RaycastHit;
if (Physics.Raycast(transform.position,transform.TransformDirection(0,0,1),hhh,1000))
{
print ("TriIndex"+hhh.triangleIndex);
//print(HOW TO PRINT TRIANGLE INDEX HAS CHANGED????????);
}
}