So i want to add an ability to my game, where you get so much money and he can buy a spud gun, to replace his bean shooter. Would i just have a shared var, and when lets say, (Spud){ Makes the mesh a spud gun}
Would that be the best approach? Just changing the mesh and the shooting options?
There are many ways of doing such a thing… the way i would use (being a relatively n00b-y person) would be this:
enum GunType {
BEANSHOOTER,
SPUDGUN,
HANDGUN, //if you wanted to get more violent :P
}
public var meshFilter : MeshFilter;
public var beanMesh : Mesh;//set in inspector
public var spudMesh : Mesh;//set in inspector
public var handgunMesh : Mesh; //set in inspector
public var gunType : GunType;
public var propertyScript : SomeScript; // set this in the inspector to be your gun properties
function Start (){
meshFilter = gameObject.GetComponent("MeshFilter");
}
function Update () {
switch (gunType){
case BEANSHOOTER:
propertyScript.fireRate = 7;
propertyScript.reloadSPeed = 3;
SwapMesh(beanMesh);
//etc, etc, etc,
break;
case SPUDGUN:
//change values, etc.
SwapMesh(spudMesh);
break;
case HANDGUN:
//change values, etc.
SwapMesh(handgunMesh);
break;
}
}
function SwapMesh ( otherMesh : Mesh) {
meshFilter.mesh = otherMesh;
}
probably add this code to the gun gameobject
then another code would change the value of gunType
function BuySpudGun (){
money -= 140;
gunTypeScript.gunType = SPUDGUN;
}
about the mesh, however; i’ll have to have a further look
EDIT: added mesh swap to first code
Ok, so how exactly is this script working. i see you have an enum, is that to select the current weapon? and when you buy somthing, all i would add is the :gunTypeScript.gunType = SPUDGUN"? and one more thing, could i replace the fire rate things to other code without messing the other functions and variables up? Sorry, i just dont fully understand this code so any explination would help. Thanks
And it says, Unkown identifier, “SPUDGUN”, “HANDGUN”, “BEANSHOOTER”
i think we got to look up enum in the reference manual :
http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=enum
my guess is you’d have to add more to that script to define those types by using something like this :
http://unity3d.com/support/documentation/ScriptReference/SerializedProperty-enumNames.html