I am not sure why, I have went over and over the code to make sure everything is right and rewatched the last 2 lessons multiple times but can not find why both of my SelectedVisual circles stay on all the time. I am very new Unity… well, Coding and game development all together. Any ideas about anything i can go back and double check are very much appreciated.
I have no done the Unity Strategy course (yet) but for people to be able to help you,
you probably need to post some code and screenshots of the editor.
Images you can just upload here, and code you can paste between grave symbols
(CTRL+SHIFT+C in the textfield)
Then it looks like this
Let’s start by taking a look at your UnitSelectedVisual.cs script and the inspector of your SelectedVisual GameObjects. Please paste the text of the script.
Hey thanks. I figured i would need to share code but (excuse my ignorance) I dont know how to do it. Can you explain a little more about how to use (CTRL+SHIFT+C)? I tried and it looked like a file that needed to be opened. Is that right?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitActionSystem : MonoBehaviour
{
public static UnitActionSystem Instance { get; private set;}
public event EventHandler OnSelectedUnitChanged;
[SerializeField] private Unit selectedUnit;
[SerializeField] private LayerMask unitLayerMask;
private void Awake()
{
if (Instance != null)
{
Debug.LogError("There's more than one UnitActionSystem!" + transform + "-" + Instance);
Destroy(gameObject);
return;
}
Instance = this;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (TryHandleUnitSelection()) return;
selectedUnit.Move(MouseWorld.GetPosition());
}
}
private bool TryHandleUnitSelection()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit raycastHit,float.MaxValue, unitLayerMask))
{
if (raycastHit.transform.TryGetComponent <Unit> (out Unit unit))
{
SetSelectedUnit (unit);
return true;
}
}
return false;
}
private void SetSelectedUnit (Unit unit)
{
selectedUnit = unit;
OnSelectedUnitChanged?.Invoke(this, EventArgs.Empty);
}
public Unit GetSelectedUnit()
{
return selectedUnit;
}
}
Figured that part out. Thanks Rusty.
As you can see, the text is significantly easier to read than the screenshot, especially for old fogeys like me. Let squinting!
My first suspicion was that perhaps you’re not assigning the unit in UnitSelectedVisual, but if this were the case, then both Units selected visuals would be turned off, not on.
Are there any errors in the console while the game is playing?
Is the UnitSelectedVisual component on the SelectedVisual with the Unit properly assigned?
Oh yes, I definitely noticed a difference. my eyes arent near what they used to be myself.
I did assign the unit as described in the lecture. I’m pretty sure its in the right place. I can toggle the SelectedVisual circle manually by turning the mesh renderer on and off. It will not reappear when I click the unit while i have it manually unchecked either. Does that help any?
No errors show up and it runs fine. I just cant get the SelectedVisual to cycle between the units.
And reason number 731 I hate screenshots of code… missed this on the first couple passes of your code:
C# is a very case sensitive language, start() won’t ever get called by the Unity callback system. Change this to Start() and it looks like you should be right as rain.
OMG! I feel real stupid. Good Catch! let me test. Thank You!
Thanks Brian! Works great now!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.