I've wanted to let the player create a "canon base" that automatically shoots some "canon ball"s but (after trying it with some Debug.Log) I can say that only code before instantiating the "canon ball" gets executed :(
It's mentioned to be a multiplayer game.
using UnityEngine.Networking;
public class PlayerScript : NewtworkBehaviour
{
public GameObject canonBase;
public Transform canonBaseSpawnPoint;
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
CmdCreateCanon();
}
}
[Command]
void CmdCreateCanon()
{
GameObject _canonBase = Instantiate(canonBase.gameObject,
canonBaseSpawnPoint.transform.position, Quaternion.identity);
_canonBase.transform.rotation = canonBaseSpawnPoint.transform.rotation;
NetworkServer.SpawnWithClientAuthority(_canonBase, connectionToClient);
}
}
//other script:
using System.Timers;
using UnityEngine.Networking;
public class CanonScript : NetworkBehaviour
{
public GameObject canonBall;
public Transform canonSpawnPoint;
public int shootAmount;
void Start()
{
shoot = new Timer(shootTime * 1000);
shoot.Elapsed += Shoot_Elapsed;
shoot.Start();
}
void Shoot_Elapsed(object sender, ElapsedEventArgs e)
{
for (int i = 0; i < shootAmount; i++)
{
//Code written down here gets executed
GameObject _canonBall = Instantiate(CanonBall.gameObject, canonSpawnPoint.transform.position, Quaternion.identity) as GameObject;
_canonBall.transform.rotation = canonSpawnPoint.transform.rotation;
//Code written down here doesn't get Instantiated
}
}
}
To add:
with Debug.Log() I found out that the code in the for loop gets executed, but everything behind the canonBall-Instantiation-code doesn't.
the canonBall is linked with a BulletScript so it gets moved (this should propably be fine because it also works on other objects).
the "...SpawnPoint"s are cubes with disabled Mesh Renderer and Box Collider and got attached to the GameObject needed to instantiate the other object (Player --> canonBase; canonBase --> canonBall)
Edit: for now I think it doesn't work because of the for loop or so? (because letting the Player directly create a canonBall (without the timer and for loop part) just works fine)