- Home /
Passing a set of variables from one script to another
I'm trying to make a simple 2.5d platformer with ranged weapons. The player has a bunch of variables (gun name, fire rate, etc.) That dictate how the current weapon functions. The problem is that, right now, I'm putting a similar set of values into every gun object, and having lines line 'player.fireRate = fireRate;' 10 times feels redundant and inefficient. Is there any way I can pass all those values at once? Like in a class or something?
I'm somewhat new to Unity, so this might seem a little noob-ish.
Answer by MitchWardle180 · Jun 03, 2015 at 08:16 PM
Which ever class you are trying to pass the variable into create a public function inside of the script, something like this
public void GetCurrentWeaponStats(String name, float fireRate, float muzzleVelocity){
this.name = name;
this.fireRate = fireRate;
this.muzzleVelocity = muzzleVelocity;
}
On my FPS project I have a player weapon controller with variables such as; muzzleVel, coolDownTime, reloadTime, bulletSound, etc.
and then on the weapon GameObject I have a script which has the same variables as player weapon controller and whenever I pick up or change weapon the PlayerWeaponController script gets the current weapon stats from of off the script on the weapon.
Answer by karma0413 · Jun 03, 2015 at 12:35 PM
Okay, I understood the first part, but the second part of your question was difficult to understand.
Here is one way: --Make sure both scripts are on gameObjects
And then simply: --find that gameobject --then access the script (as a component) of that gameObject. All those things you add into the editor window over there on the right hand panel. The transform, the collider, the mesh... all those things are components, just FYI
So if have a gameObject with all of the inventory related data. Let's just call it InventoryData for ease sake. It has this script on it called Inventory.cs. You also have to change the tag on this game object to InventoryData so that we can find it and access it quickly.
float myGunDelay;
myGunDelay = GameObject.FindGameObjectWithTag ("InventoryData").GetComponent <Inventory> ().gunDelay;
we are saying that MY gunDelay <-- I say MY because it is the gunDelay for this script, the player = Find the object we already tagged and grab it's component , that script we called Inventory...... and the we retrieve the variable inside of it called gunDelay.
How you are operating your inventory is completely unknown. You should probably look into "creating structures in c#". An easy way as I am a beginner also is to do something like this inside your inventory:
public float gunDelay;
public string gunDescription;
public int gunDamage;
void update ()
{
}
public void UpdateTheData (int gunTypePassThrough)
{
//if the gun type is 1
if (gunTypePassThrough == 1)
{
gunDelay = 2.3f;
gunDescription = "pistol";
gunDamage = 20;
}
}
And then to use it; You just simply say Okay.. I am going to make my #1 a pistol, and 2 will be a shotgun and 3 will be a laser. Or whatever... And then you just call it from within the script by typing:
UpdateTheData (1);
Or call it from outside of the script......
// if you wanted to tell the inventory script that now you have the shotgun (#2).....
GameObject.FindGameObjectWithTag ("InventoryData").GetComponent <inventory>().UpdateTheData (2);
//You could also find out what the players current gun is the same way...
myGunDelay = GameObject.FindGameObjectWithTag ("InventoryData").GetComponent <inventory>().gunDelay;
myGunDescription = GameObject.FindGameObjectWithTag ("InventoryData").GetComponent <inventory>().gunDescription;
myGunDamage = GameObject.FindGameObjectWithTag ("InventoryData").GetComponent <inventory>().gunDamage;
Answer by $$anonymous$$ · Jun 03, 2015 at 08:51 PM
I like to use structs in this case.
public struct playerthings{
int ball,
float gun,
enum ...
}
public playerthings PlayerThings;
and then..
PlayerThings.ball
I hope this helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200556im_/https://answers.unity.com/themes/thub/images/avi.jpg)