- Home /
How do I add a property to my main camera and access it elsewhere?
I have added a script to my main camera with which I want to hold a public propety that is the maximum distance to the surface of my game.
I can set this public property in unity just fine. However, when I do Camera.main, I cannot find this property anywhere. I did some digging and googling and I can't seem to find anything.
Does anyone know how to do this. I basically want to store the maximum distance on the camera and then use it for a raycast later in multiple object's scripts.
Answer by Dave-Carlile · Jun 07, 2015 at 08:33 PM
Your script is stored as a component on the camera GameObject. You can use GetComponent to get the script reference, and then access the properties using that reference. There is a tutorial for this.
e.g...
YourScript script = Camera.main.GetComponent<YourScript>();
script.MyProperty = value;
In general, if you're going to be updating properties often on the component you should store the reference to it rather than call GetComponent
every frame.
Feel free to check the checkmark to accept this answer.
Your answer
Follow this Question
Related Questions
Drag Camera only when raycasting Terrain 1 Answer
Raycast based on crosshair on screen 1 Answer
Raycast and First Person Controller 2 Answers
How to rotate a camera like Google Earth? 0 Answers
Need help for camera view like Air Strike 3d game ? 1 Answer