我對C#相當陌生(基本上第一次使用,必須爲此項目編寫腳本),我試圖傳送(從屏幕到屏幕上)一個對象我的遊戲(我使用的是一個簡單對象的立方體,當它工作時,我將使用Inkscape在它的位置創建一個'更好'的對象)稍後會添加cube2,只是專注於讓它工作。Unity 2D c#腳本,Spawnobject在鼠標位置時出現故障
目標是根據玩家點擊'Bumber'上的位置併產生鼠標位置在'Bumber'上的位置,將物體傳送到我的'Bumber'預製(地板)的位置,如果而不是'Bumber'根本沒有產卵(還沒有去檢查支票),這會引發一個事件,導致玩家輸了。
當我在之前玩遊戲,當我點擊,立方體只會despawn,然後朝我扔一個錯誤,並在光標位置不產卵在
我有我的「立方」預製(從拖分層結構放入資源文件夾,其中包含spawn腳本組件)。當我重新回到團結,我得到的錯誤:
(32,37)命名爲「魔方」並不在當前的背景下存在
(32,25),用於`UnityEngine的最佳重載的方法匹配.Object.Instantiate(UnityEngine.Object,UnityEngine.Vector3,UnityEngine.Quaternion) '具有一些無效參數
(32,25)參數#1' cannot convert
對象' 表達鍵入`UnityEngine.Object」
我已經嘗試了幾個小時來修復這個腳本,查看統一數據庫並且無濟於事。
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour {
public int trapCount;
void Start()
{
trapCount = 0;
GameObject cube =(GameObject)Instantiate((GameObject)Resources.Load("cube"));
}
void Update()
{
if (Input.GetMouseButtonDown (0))
{
Spawner();
trapCount++;
}
}
void Spawner()
{
Vector3 mousePosition = Input.mousePosition;
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(trapCount == 0)
{
Instantiate(cube, transform.position, Quaternion.identity); //getting error here, I don't care about rotation value, don't want to rotate at all, but doesn't like it, if it doesn't have anything there.
}
else if (trapCount >= 1)
{
Debug.Log("Trap limit reached!");
}
}
}
C#請,另外,如果可以的話,說明你在做什麼,千恩萬謝!
請使用unity3d標籤。 –
Thx用於標籤校正。 – Miniman