Home > Mobile >  Unity Position Different in Function Compared to Update
Unity Position Different in Function Compared to Update

Time:05-20

In this code when I'm trying to get transform.position or _attackPoint.position. The value always been (0,0,0) but when I get that transforms in Update method it prints normal.

public class MeleeWeapon : Weapon
{
    [SerializeField] private Transform _attackPoint;
    [SerializeField] private float _attackRange;
    [SerializeField] private LayerMask _enemyLayers;

    private void Update()
    {
        Debug.Log(transform.position); // Prints right
    }

    public override void Attack()
    {
        Debug.Log(transform.position); // Prints 0,0,0

        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(_attackPoint.position, _attackRange, _enemyLayers);
        Debug.Assert(hitEnemies.Length > 0); // Assertion failed
        foreach(Collider2D enemy in hitEnemies)
        {
            Debug.Log($"We hit {enemy.name}");

            CharacterStats stats = enemy.GetComponent<CharacterStats>();
            if (stats == null) continue;

            stats.ApplyDamage(damage);
        }
    }
}

Game View

I am instantiating MeleeWeapon like bellow

foreach (PivotToTransform pivot in pivotPoints)
{
    if (pivot.slot == item.initialSlot)
    {
        // Instantiate prefab
        GameObject it = Instantiate(item.prefab, pivot.point.position, Quaternion.identity);

        if (!item.isDisposable) // If item not disposable set parent transform
            it.transform.SetParent(pivot.point);

        // Set current prefab
        _currentPrefab = it;
    }
}

Here are pivots:

Pivot Points

This is the MeleeWeapon prefab:

MeleeWeapon Prefab

And I attack with

try
{
    var equippedItems = EquipmentManager.Instance.equippedItems;
    var selectedSlot = EquipmentManager.Instance.selectedSlot;

    if (equippedItems.ContainsKey(selectedSlot))
    {
        // Try to get weapon
        Item equippedItem = equippedItems[selectedSlot];
        Weapon weapon = equippedItem.prefab.GetComponent<Weapon>();
        if (weapon == null) throw new System.Exception();

        // Actualy attack
        weapon.Attack();

        if (equippedItem.isDisposable)
        {
            RemoveState state = Inventory.Instance.RemoveItem(equippedItem, 1);
            // Unequip item if removed
            switch (state)
            {
                case RemoveState.Removed:
                    EquipmentManager.Instance.UnEquip(equippedItem.initialSlot);
                    break;
                case RemoveState.Error:
                    throw new System.Exception();
            }
        }

        // Set cooldown
        sm.StartCoroutine(Cooldown(equippedItem.cooldown));
    }
    else
    {
        sm.ChangeState(sm.IdleState);
    }
}
catch (System.Exception)
{
    sm.ChangeState(sm.IdleState);
}

CodePudding user response:

Have you tried using transform.localPosition ?

The parent of the gameObject can cause that.

CodePudding user response:

Looks like there is nothing wrong with parenting stuff. When I try to attack I'm using not instantiated prefab. I changed attack script something like this and it solved.

EquipmentManager.Instance.currentPrefab.GetComponent<MeleeWeapon>().Attack();
  • Related