im new to scripting and am trying to make a aim down the sights scripts and im getting errors all the time even after i fix an error please help thanks-
First up, some housekeeping- As Christian H says, you should not post amendments to your original question as answers- it is confusing and untidy. You should always try to edit your original post (remember, only you (and moderators) can keep your posts tidy!) instead of posting copy-pastes of the question with small changes. Another thing to remember is- when posting code, four spaces == one tab. If you keep your code snippets with a minimum of four spaces before them, you will never have to worry about people raging at your bad formatting!
Anyway, on to the actual answer-
'Camera' is the static reference to the Camera class- you cannot set it to be either 'active' or 'not active' since it is not even an actual camera in your scene! In this case, I struggle to understand what you are trying to do here- even using what would usually be the correct code in this case-
Camera.main.active = false;
is strange, because doing this should completely turn off the camera while the setting is on! If you have a second camera attached to your gameObject which you want to turn on or off according to whether you are holding down Fire1 or not, then you should use the lower-case 'camera' reference- this instead finds the Camera component on the current GameObject (if it exists). Ideally, you should save draw calls by disabling the main camera while you are zoomed, so the code should look something like this:
This will only work, mind you, if the script is attached to the same GameObject as your gunCamera. You should think about having a general-purpose camera switching script for moving a single, main camera around between a set of 'camera angle' transforms, which each do whatever you need them to do, and then in your camera script you move the actual camera around by changing its parent to the desired camera angle, and then interpolating the position smoothly. This way, you don't need to keep track of a lot of camera references, and you can be sure that only one camera angle will be active at a time.
You have capitalized the A in aim in your script while it should match the variable declaration name.
Replace Aim with aim in the script , i will edit the post to include solutions to potential other problems once you update the OP with the error data.
You also need to use camera.enabled and not camera.active , since active does not exist.
By the way , you seem to be switching the same camera on and off. camera.main is the current active camera.
You should instead create a variable in which you store the aim sights camera and another one to store the main one , exemplary code below.
var sightsCamera:GameObject;
var mainCam:GameObject;
if(Input.GetMouseButtonDown(1){
sightsCamera.camera.enabled=true;
maincam.camera.enabled=true;
}
if(Input.GetMouseButtonUp(1){
sightsCamera.camera.enabled=true;
maincam.camera.enabled=true;
}
thanks here’s the script but i still have this error-
Assets/aim down sights.js(12,16): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘active’.
Try this make another camera.adjust the camera to make it as aiming sight.parent it to the main camera.then put this script to main camera.dont forget to put the gun movement script