- Home /
inherit variable from another script
Hello, i've downloaded a script to dynamic crosshair, but i want to make the if inputs on my own weapon script, i will put a part of the weapon script here:
public void Shoot() {
if(magBullets > 0 && canFire == true && !animationGun.isPlaying) {
magBullets -= 1;
canFire = false;
audio.clip = fireSound;
audio.Play();
animationGun.clip = fireAnim;
animationGun.Play();
OnShoot();
spread += spreadPerSecond * Time.deltaTime;
}
else {
spread -= spreadPerSecond * 2 * Time.deltaTime;
}
If you can see there is a variable called spread and other caled spreadPerSecond. i want to inherit that variables from the crosshair script that i have.
On weapon variables I have:
public CrossHair spread;
public CrossHair spreadPerSecond;
And on crosshair script i have:
public float spreadPerSecond = 150.0f;
[HideInInspector] public float spread;
And i have the error: Assets/Scripts/WeaponBase.cs(110,35): error CS0019: Operator *' cannot be applied to operands of type
CrossHair' and `float'
Answer by zombience · Aug 08, 2013 at 02:43 PM
If I read your example correctly, it looks like you're creating a whole new class of type CrossHair and calling it spread, and again creating another class of type CrossHair and calling it spreadPerSecond.
what you'd actually want to do is this:
public class Weapon{
public CrossHair cross;
public float spread;
public float spreadPerSecond;
void Start()
{
spread = cross.spread;
spreadPerSecond = cross.spreadPerSecond;
}
}
if you create a variable cross of class CrossHair, you then have access to any variables that are declared public inside the CrossHair class, and can access and modify them if you need to.
Your answer
Follow this Question
Related Questions
I need to make the Camera Quit snapping to center... 1 Answer
FireWeapon.cs not working 2 Answers
Drawing crosshairs and other 2D graphics onto the screen? 2 Answers
Smoothen Mouse Axis Input 2 Answers
Camera Lean 0 Answers