Hey Guys ,
I’m very new at coding so this could be something very basic but I hope you can help me out!
So my problem is that I have a Collider and a Raycaster and I want to get information from the collided object, which I managed to get but I can’t “store” it because it only contains the information I’m looking for, for one call after that it gets overwritten in the Update().
I paste my test Code here maybe you could understand from it better what I wish to achieve
void Update()
{
// True if something is hit
somethingishit = Physics.Raycast(ray,out hit,distance);
//This works fine
ray = mainCam.ViewportPointToRay(new Vector3 (0.5f,0.5f,0));
Debug.DrawRay(ray.origin,ray.direction*distance,Color.yellow);
if(somethingishit)
{
Debug.Log(initialColor.ToString(teszt));
// Store the object that was hit
previousObj = hit.collider.transform.gameObject;
Debug.Log(previousObj.name);
// The hitted object's original color
initialColor = previousObj.renderer.material.color;
// Make the hitted object colored red
// So everything works fine until here because when Update() is called again
// the initialColor will be red aswell, so in the next if statement it wont
// change a thing.
hit.collider.renderer.material.color = selectionColor;
}
// if nothing is being hit right now but something was hitted before
if (!somethingishit && previousObj != null)
{
//this should set the previously hitted object's color back to its original,when nothing is hit
previousObj.renderer.material.color = initialColor;
}
}
Thank you for your time.
Robert