- Home /
Unity is not updating my script/variables
**So I added two variables and two lines of code to my script, and now it does not update it in the inspector or when I run the game. Can someone please help. I feel like I am missing something or added something extra in my script. The two variables are the recoilX and Y. And the two lines are the ones that increase the rotationX and Y by a random number 5. Help! Thanks!*
#pragma strict
//Variables
var recoilX: float = 10;
var recoilY: float = 10;
var rotationSpeed: float = 0.03;
var holdHeight: float = -0.23;
var holdSide: float = 0.27;
var holdDistance: float = 0.52;
var cameraObject: GameObject;
var ratioHipToHold: float = 1;
var hipToAimSpeed: float = 0.1;
@HideInInspector
var ratioHipToHoldV: float;
@HideInInspector
var rotationTargetX: float;
@HideInInspector
var rotationTargetY: float;
@HideInInspector
var rotationTargetXVelocity: float;
@HideInInspector
var rotationTargetYVelocity: float;
var walkScript: PlayerMovementScript;
var aimingSensitivityGS: float = 0.25;
var aimingWalkSpeedGS: float = 0.5;
var aimFOV: float = 1;
@HideInInspector
var aimFOVV: float;
@HideInInspector
var currentFOV: float = 60;
var defaultFOV:float = 60;
@HideInInspector
var positionTargetVelocity: Vector3;
var positionMoveSpeed: float = .1;
var rateOfFire: float = 18;
@HideInInspector
var fireDelay: float = 0;
var bullet: GameObject;
var bulletSpawn: GameObject;
var ammo: int = 15;
var spreadHipFire: float = 5;
var spreadIronSight: float = 3;
function Start () {
}
function Update ()
{
currentFOV = camera.main.fieldOfView;
//Spawns the bullet
if(Input.GetButtonDown("Fire1"))
{
if(fireDelay <= 0 && ammo > 0)
{
if (bullet)
{
Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
fireDelay = 1;
cameraObject.GetComponent(MouseLookScript).rotationX += (Random.value * 5);
cameraObject.GetComponent(MouseLookScript).rotationY += (Random.value * 5);
rotationTargetY +=(Random.value - 0.5) * Mathf.Lerp(spreadIronSight, spreadHipFire, ratioHipToHold);
rotationTargetX +=(Random.value - 0.5) * Mathf.Lerp(spreadIronSight, spreadHipFire, ratioHipToHold);
ammo -= 1;
}
}
}
//Reloads the weapon
if(Input.GetButtonDown("Reload"))
{
ammo = 15;
}
fireDelay -= Time.deltaTime * rateOfFire;
//Enables/disables iron sights. Also changes look sensitivity and walk speed
if(Input.GetButton("Fire2"))
{
ratioHipToHold = Mathf.SmoothDamp(ratioHipToHold, 0, ratioHipToHoldV, hipToAimSpeed);
cameraObject.GetComponent(MouseLookScript).aimingSensitivity = aimingSensitivityGS;
walkScript.aimingWalkSpeed = aimingWalkSpeedGS;
camera.main.fieldOfView = Mathf.SmoothDamp(currentFOV, aimFOV, aimFOVV, hipToAimSpeed);
}
else
{
ratioHipToHold = Mathf.SmoothDamp(ratioHipToHold, 1, ratioHipToHoldV, hipToAimSpeed);
lookScript.aimingSensitivity = 1;
walkScript.aimingWalkSpeed = 1;
if(currentFOV != defaultFOV)
{
camera.main.fieldOfView = Mathf.SmoothDamp(currentFOV, defaultFOV, aimFOVV, hipToAimSpeed);
}
}
//Sets position
transform.position = cameraObject.transform.position + (Quaternion.Euler(rotationTargetX,rotationTargetY,0) * Vector3(holdSide * ratioHipToHold, holdHeight *ratioHipToHold, holdDistance));
//Rotates the gun with the camera
rotationTargetX = Mathf.SmoothDamp(rotationTargetX, cameraObject.GetComponent(MouseLookScript).rotationX, rotationTargetXVelocity, rotationSpeed);
rotationTargetY = Mathf.SmoothDamp(rotationTargetY, cameraObject.GetComponent(MouseLookScript).rotationY, rotationTargetYVelocity, rotationSpeed);
transform.rotation = Quaternion.Euler(rotationTargetX * -1, rotationTargetY - 180, 0);
}
//transform.position = cameraObject.transform.position + (Quaternion.Euler(rotationTargetX,rotationTargetY,0) * Vector3(holdSide * ratioHipToHold, holdHeight *ratioHipToHold, holdDistance));
I don't see anything immediately apparent, but the behavior you describe seems like it should be accompanied by a console error message...is there one?
If you change the values of the variables in the script, the Inspector won't show these updates. When you click on the gear icon next to the script name in the inspector, there is an option to Reset (also Remove Component, Edit Script) settings to the script. Be Aware that any prefabs or other variables you have set (dragged and dropped) in the inspector will be removed (reset to the script default, usually none without GetComponent running at Start).
Nope, didn't help. The inspector still contains variables that I removed. This happened after I put those two lines of code but forgot the semi-colons. When I put them in, it did not update/work. And no, there is no console error.
Answer by AndyMartin458 · Jan 21, 2013 at 06:31 AM
have you looked at what Random.value is being returned? Typically you want to use a modulus operator to put it in a certain range. Like Random.value % 360 would always return a value in the range [0,359]. Also have you made sure to save and rebuild your project? Try removing the component and adding it again.
Thanks, all I had to do was rebuild. But why do I have to rebuild sometimes and sometimes I don't?
If you save your project, then when you go back to Unity, it will build the project for you. I wouldn't expect that you had to rebuild, but rather just that you had save and build. It's usually best to build in mono develop so thay you can look for errors and warnings. $$anonymous$$ost of the time you just need to build. Sometimes though, code is weird and the way it gets compiled can cause weird errors. Hitting rebuild is a good idea when things aren't working as expected.
Uhm, this answer don't really answers the question beside the fact that it's wrong ;) Random.value will return a float value betwee 0.0f and 1.0f. If you want a specifc range, use Random.Range
Yea, I figured that out, but the problem was that I had to build it again.
@Bunny83 Yes, you're probably right about Random, but all of my information was correct. So I don't know what you mean that I'm wrong. I just said to look at what is being returned to make sure that it is what is expected. $$anonymous$$y intention was to give a checklist which did work to help solve the problem.