c# - UNity3D, return value from one class/method to another class/method in Unity3D -
in unity3d,
i have weapon script , stamina script. want drain stamina when weapon swings.
i tried code , have been playing around couple of hours.
i using unity gives me comment using new , should use add.component etc appreciate answer question well!
hopefully post bit better in terms of title , information/layout tired , low on energy. im going take short food break before it!
here stamina system:
`
public class hands : monobehaviour { public int startinghealth = 100; public int currenthealth; public int healthreg; sword mysword; bool isregenhealth; public float startingstam = 100; public float currentstam; public float stamreg; bool isregenstam; void awake() { currentstam = startingstam; } void update() { if (currentstam != startingstam && !isregenstam) { startcoroutine(regainstamovertime()); } } private ienumerator regainstamovertime() { isregenstam = true; while (currentstam < startingstam) { stamregen(); reducestamina(mysword.stamdrain); yield return new waitforseconds(1); } isregenstam = false; } public void stamregen() { currentstam += stamreg; } public void reducestamina(float _stamdrain) { currentstam -= _stamdrain; }
}
`
here sword script:
using unityengine; using system.collections; public class sword : monobehaviour { static animator anim; public gameobject hitbox; hands hp = new hands(); public int sworddamage = 20; public float sec = 0.5f; public float maxstamina = 20; public float attackcd; public float delaybetweenattacks = 1.5f; public float stamdrain = 50; public audiosource weaponsource; public audioclip weaponsound; void start () { anim = getcomponentinparent<animator>(); weaponsource = getcomponent<audiosource>(); } // update called once per frame void update () { attack(); block();
}
public void attack() { if (input.getbuttondown("fire1") && time.time > attackcd) { attackcd = time.time + delaybetweenattacks; anim.setbool("isattacking", true); hitbox.setactive(true); startcoroutine(latecall()); weaponsource.playoneshot(weaponsound); debug.log("hit"); hp.reducestamina(stamdrain); } else { anim.setbool("isattacking", false); }
}
public void block() { if (input.getbuttondown("fire2")) { anim.setbool("isblocking", true); } else { anim.setbool("isblocking", false); } } ienumerator latecall() { yield return new waitforseconds(sec); hitbox.setactive(false); }
}
you can create getter/setter methods set/get attributes class a. in class b, can create instance o class , access attibutes.
example class a:
public class genre { public string name { get; set; } }
example instance in class b:
genre genre = new genre(); string name = genre.getname();
hope helps.
Comments
Post a Comment