What would be causing unit selected visuals to bug in this manner?

Up to the unit selected visual, singleton chapter and I found that I could get the visuals to change but only on a second click. The first click would choose the correct unit but the visual would not change unless I clicked on the unit a second time. To remedy this, I added the UpdateVisual(); method to update and it works perfectly, but I am curious as to why it would be doing that in the first place?

We would have to see your code to find the problem. Perhaps show the UnitActionSystem and the UnitSelectedVisual scripts and we’ll see.

The issue is just with the visual? If you click to select a Unit it does select that unit and you can take actions with that Unit but the visual is still on the previous Unit?
Perhaps you have some issue with how you are firing the event. Add a Debug.Log when firing the event and on the visual when listening.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class UnitSelectedVisual : MonoBehaviour
{

   [SerializeField] Unit unit;
    MeshRenderer meshRenderer;

    private void Awake()
    {
        meshRenderer = GetComponent<MeshRenderer>();
    }

    private void Start()
    {
       UnitActionsSystem.Instance.OnSelectedUnitChange += UnitActionSystem_OnSelectedUnitChange;
       UpdateVisual();
    }

    private void Update()
    {
        UpdateVisual();
    }

    private void UnitActionSystem_OnSelectedUnitChange(object sender, EventArgs empty)
    {
        UpdateVisual();
    }

    private void UpdateVisual()
    {
        if (UnitActionsSystem.Instance.GetSelectedUnit() == unit)
        {
            meshRenderer.enabled = true;
        }
        else
        {
            meshRenderer.enabled = false;
        }
    }
}

||

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class UnitActionsSystem : MonoBehaviour
{
    public static UnitActionsSystem Instance{get; private set;}
   

    [SerializeField] Unit selectedUnit;
    [SerializeField] LayerMask unitsLayerMask;
    public event EventHandler OnSelectedUnitChange;

       

    private void Awake()
    {
      if(Instance != null)
      {
        Debug.LogError("There's more than 1 UnitActionSystem" + transform + " - " + Instance);
        Destroy(gameObject);
        return;
      }
      Instance = this;
    }

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {  
            if(TryHandleUnitSelection()) return;
            selectedUnit.Move(MouseWorld.GetPosition());
            Debug.Log("Selected Unit!");
        }  
    }


      private bool TryHandleUnitSelection()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out RaycastHit raycastHit, float.MaxValue, unitsLayerMask))
        {
            if(raycastHit.transform.TryGetComponent<Unit>(out Unit unit))
            {
                SetSelectedUnit(unit);
                return true;
            }
        }
        return false;
    }

    void SetSelectedUnit(Unit unit)
    {
        OnSelectedUnitChange?.Invoke(this, EventArgs.Empty);
        selectedUnit = unit;
    }

    public Unit GetSelectedUnit()
    {
        return selectedUnit;
    }
}

ah sorry im trying to figure out how to format this response.

AFAIK it’s purely a visual bug. Clicking on units still lets me take actions with them without issue,

Switch these two lines. You are letting everyone know the unit changed before the unit changed. The event listener in UnitSelectedVisual is updating the visual when the selected unit is still the old unit

1 Like

Ah thanks so much for that! Really appreciate it,

All fixed!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

I’ve taken the liberty of formatting your code post for you. If you click on the edit button of that post, you’ll see that I put three backwards apostrophe’s on lines by themselves before and after the code. I also removed the extra spaces, which weren’t in your original code but inserted by the forum software when it interpreted each line of code as it’s own paragraph.

Example:
```
Some Code
```
becomes

Some Code

Privacy & Terms