0

In Unity3D,

I have a weapon script and a stamina script. What I want to do now is to drain stamina when my weapon swings.

I tried my code and i have been playing around with it for a couple of hours.

I am using unity so it also gives me a comment about using new and that I should use Add.component etc so I would appreciate an answer to that question as well!

Hopefully this post is a bit better in terms of the title and information/layout as I am very tired and low on energy. Im going to take a short food break before I get back to it!

Here is the 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 is the 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 is 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);
}

}

Fattie
  • 27,874
  • 70
  • 431
  • 719
CSharpNoob
  • 13
  • 4
  • Possible duplicate of [Simple event system in Unity](http://stackoverflow.com/questions/36244660/simple-event-system-in-unity) – Fattie Jul 25 '16 at 19:00
  • Hi C-Noobster. What you want to learn about next is **UnityEvent**. Be sure to read through the linked question. It is very easy. – Fattie Jul 25 '16 at 19:02
  • Be sure to do the tutorial on the linked question, enjoy – Fattie Jul 25 '16 at 19:02
  • I will definitely read it now as I am eating. I would appreciate if you had a direct answer for my question so I could learn from my example rather than applying it from someone else. That way I could understand much better and faster of how things work. If not then I thank you for the time you took to answer my question and hopefully the answer you provided will teach me about the unityevent system and the way it can help me solve my issue! – CSharpNoob Jul 25 '16 at 19:14
  • Hi CSharp. I don't know how clear I can be. I wrote the linked answer. here: http://stackoverflow.com/questions/36244660/simple-event-system-in-unity I could literally copy and paste the text here if you like :) – Fattie Jul 25 '16 at 19:15
  • There are a number of ways to achieve stuff in Unity, and that may be easiest and best for you. – Fattie Jul 25 '16 at 19:16
  • Indeed there are how ever since I am brand new to coding in general this whole eventsystem has me scratching my head. I have no clue how to connect my weapon to drain stamina from the stamina script using the system you provided. – CSharpNoob Jul 25 '16 at 20:05
  • OK I'm sure someone else will help you. Good luck! – Fattie Jul 25 '16 at 20:09

1 Answers1

0

You can create getter/setter methods to set/get attributes from the Class A. In your class B, you can create an instance o class A and access to that 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 it helps.

Fattie
  • 27,874
  • 70
  • 431
  • 719
Angel Guevara
  • 428
  • 4
  • 14
  • This is a **Unity3D** question - this is not relevant, Angel. The question here relates to the GameObject constructs in Unity – Fattie Jul 25 '16 at 18:59
  • Joe Blow, you have modified the question and you have added the Unity3D tags. I don't know why you vote me down if you knew the original question only had the C# tag. So sad. – Angel Guevara Jul 25 '16 at 19:49
  • Hi @angelGuevara ! No no, I did not add the Unity3D tag! ***I just added it to the headline of the question, to save anyone else wasting time.*** Note that the question is **absolutely** about Unity3D, and nothing else. – Fattie Jul 25 '16 at 20:00
  • this is a constant problem on this site: c# experts answer a Unity question not realizing it is a Unity question. it's a huge waste of your time! – Fattie Jul 25 '16 at 20:01
  • I did add the unity3d tag how ever I will add it to the title as well from now on. – CSharpNoob Jul 25 '16 at 20:06