[solved]Need help on accessing gameobject in another script

This is maybe a simple question but i did search but didnt come up with results.

So if I have a gameobject X with 2 scripts, A and B.

script A has reference to another game object Y.

Is there a way for script B to reference to object Y through script A?

i have tried GetComponent().objectY but it didnt worked.

Please help on this.

Yes, you need to study some fundamentals of programming, but don’t be discouraged. I struggled with this too.

What did the errors read? Note that the brackets are a , just like float, and that GetComponent returns an instance of a script.

Solved it. Looks like I shouldnt declare GetComponent in awake. Moved it to start and everythings smooth now.

Strange. I swear people always get references within Awake().
Are you sure you changed nothing else?


https://www.google.co.uk/?gws_rd=ssl#q=unity+reference+script

He probably has something happening in Awake in Script A like getting the object reference. Referencing Script A during Awake could have missing references. Initializing Script B in Start could solve that.

If you have scripts attempting to reference each other, note that Edit → Project Settings → Script Execution Order determines what order your scripts will be run in.

Sorry my other reply was sort of vague, it was 12:30am and I abruptly realized I had stayed up too late and bailed half way through. Next time just post both scripts though, people will be able to give your more specific answers and it’ll help you learn more. Nobody’s needing to steal your scripts and nobody will judge you for early bad designs, as long as you can later show you improved.