Start with some basic casting tutorials. No sense one of us trying to retype it all.
Also, read specifically in the docs for what that particular method does and all the requirements for it to function.
Otherwise, this smells like an XY Problem, so perhaps you should start with what you are trying to do.
How to report your problem productively in the Unity3D forums:
This is the bare minimum of information to report:
- what you want
- what you tried
- what you expected to happen
- what actually happened, log output, variable values, and especially any errors you see
- links to documentation you used to cross-check your work (CRITICAL!!!)
Don’t TALK about code without posting it. Don’t RETYPE code. Copy/paste and post code properly. ONLY post the relevant code, and then refer to it in your discussion.
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly