Caching a reference in other areas?

So I was experimenting with the code to see if I really understood what I was seeing and I bumped into something I’m really curious about. I was wondering if there is a good explanation for why this is…

I set my cache references to:
MeshRenderer render;
Rigidbody rigid;

When I was typing out the GetComponent section I thought to myself:
“I should be able to put this outside of Start() because it’s just a simple declaration that (x name) = (y command), right?”
…but the names render and rigid appear white instead or blue and had to be put in Start() for them to work. Why is that?

How did you write this? There is no reason why you can’t put it anywhere other than Start(). It’s just code

At a guess, you’re putting in where you declare the variables. If so, you can’t do it there.

You can put it inside and method you want though.

Indeed. When I said ‘anywhere other than Start()’ I meant another function… My bad

I gathered. I was just reading his problem between the lines. Not sure if I was even close to the mark.

Privacy & Terms