0
votes

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)

1
Unless unity is significantly different from the c# standard, I can't see how this code would even compile.theMayer
sry, I'm a pretty newbie in scripting with C# and just simplifying code, but the raw code works (except for the canon instantiating some canon balls :( )Panzer0312
(I added some of the raw code so now it might be a bit better :)Panzer0312
At least it will compiletheMayer

1 Answers

0
votes

You can make Start a coroutine, like this.

using System.Collections;
using UnityEngine;


public class Cannon : MonoBehaviour {

    [SerializeField] private GameObject _cannonballPrefab;


    private IEnumerator Start () {
        yield return new WaitForSeconds (2);
        Instantiate (_cannonballPrefab);
    }
}

When you instantiate a cannon with this script attached, a cannonball will be created after a 2-seconds delay.