亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

實例化對象不實例化其他對象

實例化對象不實例化其他對象

C#
狐的傳說 2022-08-20 17:27:07
我想讓玩家創建一個“佳能基地”,自動拍攝一些“佳能球”,但(在嘗試了一些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循環部分)只是工作正常)
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您可以制作一個協程,如下所示。Start


using System.Collections;

using UnityEngine;



public class Cannon : MonoBehaviour {


    [SerializeField] private GameObject _cannonballPrefab;



    private IEnumerator Start () {

        yield return new WaitForSeconds (2);

        Instantiate (_cannonballPrefab);

    }

}

當您在附加了此腳本的情況下實例化大炮時,將在延遲 2 秒后創建一個炮彈。


查看完整回答
反對 回復 2022-08-20
  • 1 回答
  • 0 關注
  • 91 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號