I need to call something only once in Update(). (??)

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

First of all, you should be aware that - as your Update() is called every single frame, - it is most likely that the Ray will return the same target repeatedly (I don’t assume that your camera changes orientation with every frame).
This means that the following will happen:

In frame 1, the lines

previousObj = hit.collider.transform.gameObject;
initialColor = previousObj.renderer.material.color;

will assign the initialColor as intended (let’s assume the original color is BLUE)

Then, further below, the line

hit.collider.renderer.material.color = selectionColor;

will assign your new color (RED) to the target object (as intended).

So far, so good. At this point in time, initialColor contains BLUE, and the object is RED (as intended).

Then, however, in frame 2, the first two lines will set initialColor to RED again, because the target hit (previousObj) is still the same object, which now actually is RED.
Consequently, the second code part resets the same object’s color back to RED.
At that point, both initialColor and the object are RED (you’ve got yourself caught in a RED trap :)).

So what’s the solution?

As I assume that you need the initialColor (e.g. for restoring the various targets’ colors later), you should find a way to store initialColor for each object separately, e.g. in an array.
An easy (albeit not very elegant) approach would be to maintain two different arrays (with numeric indexes), one storing the hit objects, and one storing their initial colors. Something like…

private GameObject[] hitTargets;
private Color[] initialColors;

You then have to ensure two things:

  1. That for every hit target, both the object and its color are stored in these arrays, with the same(!) index. A nicer solution would be to use a multidimensional array, but I am not too familiar with it in Unity scripting yet
  2. That BEFORE changing an object’s color, you have to check whether it has already been hit (by checking against the array)

I cannot go much more into the details right now. But you should spend some thoughts on getting familiar with arrays (if not already done).

Maybe my solution is not very state-of-the-art, but I hope that I could at least explain the cause of the problem!