- Home /
Failure generating network code
Hello guys, i don't know how to fix it i tried everything pls help
here is my script i made
#pragma strict
import UnityEngine.Networking;
public var object:GameObject;
public var iden:UnityEngine.Networking.NetworkIdentity;
public var shotObj:GameObject;
function Start () {
iden = object.GetComponent(UnityEngine.Networking.NetworkIdentity);
}
function Update () {
if(!iden.isLocalPlayer){
return;
}
if(Input.GetKeyDown("e")){
CmdFire();
}
}
@Command
public function CmdFire(){
Instantiate(shotObj, transform.position, transform.rotation);
}
Answer by Bunny83 · Jul 23, 2016 at 11:06 AM
Just like the error said. To be able to use "@Command" and Unity's network features your class need to be derived from NetworkBehaviour instead of MonoBehaviour. Since a script file in UnityScript automatically creates a class that is derived from MonoBehaviour you have to create the class manually in order to derive it from NetworkBehaviour:
// shot.js
#pragma strict
import UnityEngine.Networking;
public class shot extends NetworkBehaviour
{
public var object : GameObject;
public var iden : NetworkIdentity;
public var shotObj : GameObject;
function Start()
{
iden = object.GetComponent(NetworkIdentity);
}
function Update ()
{
if(!iden.isLocalPlayer){
return;
}
if(Input.GetKeyDown("e")){
CmdFire();
}
}
@Command
public function CmdFire()
{
Instantiate(shotObj, transform.position, transform.rotation);
}
}
Keep in mind that the classname has to match the filename.
Your answer
Follow this Question
Related Questions
Why I cant use 2 NetworkManagers in the same time? 0 Answers
Network, Builds wont move 1 Answer
Can't access "NetworkServer" 0 Answers
How do I get the network delay between server and client? 1 Answer
[Command] tag not working 0 Answers