- Home /
How can I assign a GameObject to a variable at runtime?
how to change the game object in the run time??? is it hard listen this
var targer = GameObject"
how can i change the game object in the run time
lets say
i am the camera and i shot a bullet toward a cube when it hit it i want it to put the cube In the game object on the run time
or to hit it with the ray cast instead of bullet. i hope u got it....
Ok, we're going to need some clarification here. Are you asking how to 'shoot' a game object and then place the object that's hit into a variable?
its to make a targeting system to a plane aim with camera when i aim at at target it put it in the gameobject variable
i got an idea for it. look i made a gun script! i will put it in the camera and make it shoot all the time and when it hit something it change the variable gameObject in the ho$$anonymous$$g missile script (The $$anonymous$$issile Target). i shoot It chase it and going down.
if u could make it with raycast it would be better..
Please try to form sentences. It's hard enough to understand exactly what's going on without having to parse your words manually, too.
Are you saying you've fixed your problem?
Answer by Chris D · Jun 22, 2011 at 05:52 PM
You should look at the Raycast functions available. All you need to do is raycast from the camera's transform.position to where your mouse/selector/whatever is currently on.
Search through the other raycast questions on the site for a very complete listing of different approaches. Search terms include 'mouse click', 'raycast', 'select' etc.
Once you successfully raycast to something, you use the RaycastHit information to reference the object that's hit.
var whatYouHit = RaycastHit.rigidbody.gameObject;
If you don't understand how to do what I've mentioned above, try going through some of the tutorials (I believe there's some raycasting going on in the FPS tutorial) and then looking at the manual on the Unity site.
If you're still stuck, come back to this site and create a new question about your specific problem.