In class called Player i created an enum
public enum weapons
{
none,
katanaEquipped,
macheteEquipped,
longSwordEquipped,
crossBowEquipped,
}
then i set weapons myWeapon = weapons.none;
and i check which weapon is equipped and i tell it to set it to proper weapon, for example
if (machete)
{
myWeapon = weapons.macheteEquipped;
Debug.Log(myWeapon);
And in other script called WeaponAnimator
i created reference to player script Player.weapons playerWeapons;
if (playerWeapons == Player.weapons.macheteEquipped)
{
{
Debug.Log("machete equipped");
if (Input.GetMouseButton(0))
{
weaponAnim.SetBool("IsAttacking", true);
Debug.Log("attack");
}
else
{
weaponAnim.SetBool("IsAttacking", false);
Debug.Log("You are not attacking.");
}
}
}
but it does not work can you give me some advices what causes that?
Instead of
weapons myWeapon = weapons.none;
try having something like
weapons CurWeaponState;
Start()
{
CurWeaponState = weapons.none
}
Then when you call it in other functions to say you have a machete
gameObject.GetComponent<Player>().CurWeaponState = weapons.macheteEquipped;
This is how i do a state machine, and it works for me
Baste
May 3, 2016, 10:19am
3
The playerWeapons variable in the WeaponAnimator has the same type as the myWeapon variable in your Player script, but that doesn’t mean that they’re connected in any way.
If you want the WeaponAnimator to use the weapon field from the Player, you’ll need to connect the WeaponAnimator with the Player, and use it’s variable.
Baste:
The playerWeapons variable in the WeaponAnimator has the same type as the myWeapon variable in your Player script, but that doesn’t mean that they’re connected in any way.
If you want the WeaponAnimator to use the weapon field from the Player, you’ll need to connect the WeaponAnimator with the Player, and use it’s variable.
isnt that Player player; a connection?
then i tried to do
if(player.myWeapon == player…weapons.macheteEquiped){}
but that did not work , first of all player. <== it doesnt even show me myWeapon as an option to write
Baste:
The playerWeapons variable in the WeaponAnimator has the same type as the myWeapon variable in your Player script, but that doesn’t mean that they’re connected in any way.
If you want the WeaponAnimator to use the weapon field from the Player, you’ll need to connect the WeaponAnimator with the Player, and use it’s variable.
so why do you think my solution does not work?
It might be private, you do post:
weapons myWeapon = weapons.none;
Fields default to private in classes.
lordofduct:
It might be private, you do post:
weapons myWeapon = weapons.none;
Fields default to private in classes.
i made it public , still does not work
in weaponAnimator in 23 line i get error "
NullReferenceException: Object reference not set to an instance of an object
WeaponAnimator.Update () (at Assets/WeaponAnimator.cs:23)
"
Sounds to me like the ‘player’ property is null…
if you’re getting a null reference exception, SOMETHING is null.
It says it’s happening at line 23, which is:
if(player.myWeapon== Player.weapons.macheteEquiped)
And the only thing here that could be null is ‘player’, since the rest of it is just namespace.
So either ‘player’ is null, or that’s not the line that the error is happening on.