Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Sericet · Jan 08, 2012 at 11:41 PM · variablesstatic

Call variable from other script but not allow it to be updated by that script

I have a weapon manager like so

#pragma strict

static var WeaponSelected : int = 1;

//Shotgun

var RateOfFireShotgun : float =1;

var RoundsShotgun : int = 5;

var ReloadTimeShotgun: float = 5;

//Magnum

var RateOfFireMagnum : float = 0.8;

var RoundsMagnum : int= 8;

var ReloadTimeMagnum: float = 4;

//Magnum

var RateOfFireMachineGun : float = 0.1;

var RoundsMachineGun : int = 30;

var ReloadTimeMachineGun: float = 3;

static var RateOfFire: float;

static var Rounds: int;

static var ReloadTime: float;

function Update ()

{

if(WeaponSelected == 1) //Shotgun

{

RateOfFire = RateOfFireShotgun;

Rounds = RoundsShotgun;

ReloadTime = ReloadTimeShotgun;

}

 if(WeaponSelected == 2)//Magnum
 {
     RateOfFire = RateOfFireMagnum;
     Rounds = RoundsMagnum;
     ReloadTime = ReloadTimeMagnum;
 }
 
 if(WeaponSelected == 3)//MachineGun
 {
     RateOfFire = RateOfFireMachineGun;
     Rounds = RoundsMachineGun;
     ReloadTime = ReloadTimeMachineGun;
 }

} I am calling the Static vars from my Gun script which will need the values of Rounds, RateOfFire, and ReloadTime.

Problem I am having is that the Rounds keeps updating to it orginal value like it shoulod (but I don't want it to).

I really like this script as it is easy to change the values and seems to allow me to swtich weapons very easily.

How can I get the current value of the Rounds from this script and not have it be effected from this script anymore? (like make a copy of its current value).

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Jan 09, 2012 at 01:04 AM

Since you're defining the variables every Update, any change will be overwritten. The solution is to reload the variables only when the weapon is switched, what you can detect using an auxiliary variable lastWeapon. You could also use a switch statement instead of several *if*s - works the same, but is a little faster and looks better:

#pragma strict

static var WeaponSelected : int = 1;

//Shotgun var RateOfFireShotgun : float =1; var RoundsShotgun : int = 5; var ReloadTimeShotgun: float = 5;

//Magnum var RateOfFireMagnum : float = 0.8; var RoundsMagnum : int= 8; var ReloadTimeMagnum: float = 4;

//MachineGun var RateOfFireMachineGun : float = 0.1; var RoundsMachineGun : int = 30; var ReloadTimeMachineGun: float = 3;

static var RateOfFire: float; static var Rounds: int; static var ReloadTime: float; private var lastWeapon: int = -1; // force loading variables in the first Update

function Update () { if (WeaponSelected != lastWeapon){ // if WeaponSelected changed... lastWeapon = WeaponSelected; // update lastWeapon... switch (WeaponSelected){ // and redefine variables case 1: //Shotgun RateOfFire = RateOfFireShotgun; Rounds = RoundsShotgun; ReloadTime = ReloadTimeShotgun; break; case 2: //Magnum RateOfFire = RateOfFireMagnum; Rounds = RoundsMagnum; ReloadTime = ReloadTimeMagnum; break; case 3: //MachineGun RateOfFire = RateOfFireMachineGun; Rounds = RoundsMachineGun; ReloadTime = ReloadTimeMachineGun; break; } } }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sericet · Jan 09, 2012 at 01:28 AM 0
Share

Thank you!

avatar image
3

Answer by syclamoth · Jan 09, 2012 at 01:31 AM

This isn't really related to the question, but you should really restructure your classes so that it's neater (and has less copy-pasted code!)

Make a class, 'Weapon', at the top.

 public class Weapon
 {
     var rateOfFire : float;
     var maxRounds : int;
     var reloadTime : float;

     // possibly you may also want a 'currentRounds' value
     var currentRounds : int;
 }

Then, where you had all that 'reloadTimeShotgun, reloadTimeMagnum' etc, just put-

 var shotgun : Weapon;
 var magnum : Weapon;
 var machineGun : Weapon;

 private var currentWeapon : Weapon;

When you switch weapons, use

 currentWeapon = shotgun;

or

 currentWeapon = machineGun;

and then for your 'RateOfFire' or 'Rounds' just use

 currentWeapon.rateOfFire;

and

 currentWeapon.maxRounds;

As for you original question, instead of decreasing a weapons maximum ammo capacity when it fires, decrease the 'currentRounds', which makes sure that it doesn't forget the original max value.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sericet · Jan 09, 2012 at 01:47 AM 0
Share

Thanks again, I''m new to program$$anonymous$$g and don't really get classes. I get this tho! Working on a project and I'm sure my code looks silly, 7 scripts now.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find which scripts public variables appear in? 1 Answer

Script variable can be referenced in Start but not in update. 1 Answer

Static Variables 1 Answer

Be able to choose which variable I want from another scripts public variable 2 Answers

Telling the difference between static variables being pulled from several scripts of the same name on different objects. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges