实例化对象不实例化其他对象

我想让玩家创建一个“佳能基地”,自动拍摄一些“佳能球”,但(在尝试了一些Debug.Log之后),我可以说,只有在实例化“佳能球”之前的代码才会执行:(


它被提及为多人游戏。


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 

  }

 }

}

要添加:


使用Debug.Log()我发现for循环中的代码被执行,但canonBall-Instantiation-code后面的所有内容都没有。


canonBall与BulletScript链接,因此它被移动(这应该没问题,因为它也适用于其他对象)。


“...SpawnPoint是具有禁用网格渲染器和Box Collider的立方体,并附加到实例化其他对象(玩家 - >canonBase; canonBase - >canonBall所需的游戏对象)


编辑:现在我认为它不起作用,因为for循环左右?(因为让玩家直接创建一个canonBall(没有计时器和for循环部分)只是工作正常)


狐的传说
浏览 72回答 1
1回答

HUH函数

您可以制作一个协程,如下所示。Startusing System.Collections;using UnityEngine;public class Cannon : MonoBehaviour {&nbsp; &nbsp; [SerializeField] private GameObject _cannonballPrefab;&nbsp; &nbsp; private IEnumerator Start () {&nbsp; &nbsp; &nbsp; &nbsp; yield return new WaitForSeconds (2);&nbsp; &nbsp; &nbsp; &nbsp; Instantiate (_cannonballPrefab);&nbsp; &nbsp; }}当您在附加了此脚本的情况下实例化大炮时,将在延迟 2 秒后创建一个炮弹。
打开App,查看更多内容
随时随地看视频慕课网APP