2014-05-24 140 views
0

我對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#請,另外,如果可以的話,說明你在做什麼,千恩萬謝!

+0

請使用unity3d標籤。 –

+0

Thx用於標籤校正。 – Miniman

回答

0

總是(好,幾乎總是)相信錯誤信息告訴你什麼。

(32, 37) the name 'cube' doesn't exist in current context

你得到這個爲線

Instantiate(cube, transform.position, Quaternion.identity); 

在這一點上沒有cube的方法的範圍內的任何地方。你有你的

GameObject cube =(GameObject)Instantiate((GameObject)Resources.Load("cube")); 

在你的Start()方法,但它只存在於該方法。例如,它不是該類的成員。讓它成爲會員將解決這個問題。

而且這很可能也是後續錯誤的原因。如果它不知道多維數據集是什麼,它不知道如何處理它作爲Instantiate()的參數。

如果你對C#完全陌生,那麼你自己可以做的最大的好處就是拿起一本好書。 Unity可以讓你遠離黑客攻擊,但是會有一個地方無法替代學習語言。它會幫助你很大。

祝你好運。