Help with selecting objects through raycast in multiplayer game
Hello, i've been programming for some time now and i decided to start a multiplayer project. It's a 2D rpg game where you battle by selecting enemies and then cast abilities on them. So far everything has gone smoothly by following some toturials and applying what i learned from them to the project, but now i seem to be stuck with an error. The error is: UNetWeaver error: Command function [Raycast:CmdClickSelect] must have a void return type. UnityEngine.Debug:LogError(Object) Unity.UNetWeaver.Log:Error(String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:20) Unity.UNetWeaver.NetworkBehaviourProcessor:ProcessMethods() (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetBehaviourProcessor.cs:1240) Unity.UNetWeaver.NetworkBehaviourProcessor:Process() (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetBehaviourProcessor.cs:56) Unity.UNetWeaver.Weaver:ProcessNetworkBehaviourType(TypeDefinition) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1064) Unity.UNetWeaver.Weaver:CheckNetworkBehaviour(TypeDefinition) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1549) Unity.UNetWeaver.Weaver:Weave(String, IEnumerable`1, IAssemblyResolver, String, String, String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1658) Unity.UNetWeaver.Weaver:WeaveAssemblies(IEnumerable`1, IEnumerable`1, IAssemblyResolver, String, String, String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1748) Unity.UNetWeaver.Program:Process(String, String, String, String[], String[], IAssemblyResolver, Action`1, Action`1) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:34) UnityEditor.Scripting.Serialization.Weaver:WeaveUnetFromEditor(String, String, String, String, Boolean)
And it also comes out saying: Failure generating network code. UnityEditor.Scripting.Serialization.Weaver:WeaveUnetFromEditor(String, String, String, String, Boolean)
here's the code:
using UnityEngine; using System.Collections; using UnityEngine.Networking;
public class Raycast : NetworkBehaviour {
public GameObject SelectedEnemy;
[SerializeField]
private Frost frostAbility;
[SerializeField]
private Fireball fireballAbility;
[SerializeField]
private Shockwave shockwaveAbility;
public bool casting = false;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
//Raycast
Vector2 rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(rayPos, Vector2.zero, 0f);
CmdClickSelect(hit.collider.gameObject);
}
if (Input.GetKeyDown("1") && SelectedEnemy != null && casting == false)
{
StartCoroutine(CastTime(frostAbility.castTime, frostAbility.manaCost, frostAbility.abilityName));
casting = true;
}
if (Input.GetKeyDown("2") && SelectedEnemy != null && casting == false)
{
StartCoroutine(CastTime(fireballAbility.castTime, fireballAbility.manaCost, fireballAbility.abilityName));
casting = true;
}
if (Input.GetKeyDown("3") && SelectedEnemy != null && casting == false)
{
StartCoroutine(CastTime(shockwaveAbility.castTime, shockwaveAbility.manaCost, shockwaveAbility.abilityName));
casting = true;
}
}
//wait for casttime
IEnumerator CastTime(float _castTime, float _manaCost, string _abilityName)
{
yield return new WaitForSeconds(_castTime);
CmdDealEnemyDamage(GetComponent<Player>().magic, _manaCost, _abilityName);
casting = false;
}
//send damage to the selected target
[Command]
void CmdDealEnemyDamage(float _damageAmount, float _manaCost, string _abilityName)
{
if (GetComponent<Player>().currentMana > _manaCost)
{
Debug.Log("Used " + _abilityName);
SelectedEnemy.GetComponent<Enemy>().RpcEnemyTakeDamage(_damageAmount);
GetComponent<Player>().currentMana -= _manaCost;
}
else
{
return;
}
}
//this is where the problem is
[Command]
public GameObject CmdClickSelect(GameObject _hit)
{
//if enemy is hit select that enemy
if (_hit && _hit.gameObject.tag == "Enemy" && SelectedEnemy != _hit.gameObject)
{
SelectedEnemy.GetComponent<Enemy>().CmdEnemySelectPlayer(this.gameObject);
Debug.Log(_hit.gameObject.name);
return _hit.gameObject;
}
return null;
}
}
So if anybody can help me with this error or if someone has a better way of doing this then please answer. Also feel free to tell me if you see i have done something stupid in the script :)
Your answer
Follow this Question
Related Questions
Raycast Hit on a Server Object? 0 Answers
How can you spawn objects on network using raycast? 0 Answers
Raycast doesnt work for client in netcode 0 Answers
cannot reference parent network identity.netId when applying damage 1 Answer
Understanding UNET 0 Answers