[英]Variables in other scripts in Unity
我正在嘗試在 Unity 2d 中創建游戲。 我已經完成了我想做的大部分事情,並且已經轉向了敵人。 敵人(龍)從屏幕的不同點進來。 為此,我將精靈游戲對象放置在我希望龍生成的地方。 我已將所有這些對象作為另一個名為 DragonAncores 的對象的子對象。 我在 DragonAncores 上附上了一個腳本,上面寫着這個......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonTracker : MonoBehaviour {
// is gold dragon in play?
public bool GoldDragonInit = false;
// curently active dragons
public int DragonCount = 0;
// defalts to 5
public int Difficulty = 5;
}
然后我將一個腳本附加到每個精靈,最終會在龍預制件(包含 2 個對撞機和一個動畫師)中召喚,偏向於從其他變量派生的 If 語句邏輯。
下面是我正在使用的代碼。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dragons : MonoBehaviour {
// same as GoldDragonInit
bool GoldDragonSpawn = false;
// same number as DragonCount in DragonTrackeer
int LiveDragons;
// same as Difficulty
int DifLev;
//get cariables from other script
DragonAncors.cs.GetComponent.<DragonTracker>() GoldDragonInit = GoldDragonSpawn;
System.Random RNG= new System.Random();
void update()
{
RSpawn=RNG.Next(0,2)
DragonType=RNG.Next(0,101)
if (RSpawn = 1) ;
{
if (LiveDragons > DifLev) ;
{
if (DragonType > 99) ;
{
// summon regular dragon
}
if (DragonType = 100) ;
{
if (GoldDragonSpawn = true) ;
{
// summon gold dragon
}
}
}
}
}
}
這是拋出這個錯誤列表。
這顯示了我的統一層次結構和錨點(Squair 十字准線看起來的東西)
我已經尋找了解決這個主題的其他線程,他們都嘗試了不同的方法,但都沒有奏效。
我正在使用 Unity 2018.2.18f1
//get cariables from other script
DragonAncors.cs.GetComponent.<DragonTracker>() GoldDragonInit = GoldDragonSpawn;
為什么? 因為它不在方法內部。
另外,評論是錯誤的。 它沒有得到一個變量(也是錯字),它在另一個腳本中設置了一個變量!
Visual Studio 抱怨的第一個...16 個問題的原因是由於這一行。
在此位置,您只能聲明方法、字段和屬性,並且您當前正在嘗試訪問另一個類並更改其成員之一(您只能在方法內部執行此操作)。
此外,您有.cs
,我認為這是因為“DragonAnchors.cs 是文件名!” 你不需要這樣做。 我不確定如何重寫這一行(在Start()
),因為我不確定您實際上要做什么。 也就是說,我不知道 DragonAnchors 的實例實際駐留在何處。 您正在調用GetComponent()
,它通常保留用於訪問附加到游戲對象的組件,但是當您嘗試在靜態類引用上這樣做時,我不確定您是否打算在this
或上調用它別的東西。
您可以通過以下方式訪問 DragonTracker:
DragonTracker dt = GameObject.Find("DragonAncores").GetComponent<DragonTracker>()
Debug.Log(dt.DragonCount);
您的代碼中存在一些錯誤。 以下說法不正確。
//get cariables from other script
DragonAncors.cs.GetComponent.<DragonTracker>() GoldDragonInit = GoldDragonSpawn;
訪問它的正確方法,正如您所說的 DragonAncors 是父級將是:
GetComponentInParent<DragonTracker>().GoldDragonInit = GoldDragonSpawn;
這將 GoldDragonInit 布爾值設置為 GoldDragonSpawn 的值。 這必須在函數內部,因為您在函數外部擁有它,我認為您在開始時需要此設置。 因此我把它放在了 void Start() 函數中。 這在游戲開始時調用(加載場景)。
你也不需要分號; 在 if 語句之后,但是它確實需要出現在每一行不同的代碼之后。 您提供的代碼應該如下所示。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dragons : MonoBehaviour {
// same as GoldDragonInit
bool GoldDragonSpawn = false;
// same number as DragonCount in DragonTrackeer
int LiveDragons;
// same as Difficulty
int DifLev;
void Start()
{
// variables from other script
GetComponentInParent<DragonTracker>().GoldDragonInit = GoldDragonSpawn;
}
System.Random RNG= new System.Random();
void update()
{
RSpawn=RNG.Next(0,2);
DragonType=RNG.Next(0,101);
if (RSpawn = 1)
{
if (LiveDragons > DifLev)
{
if (DragonType > 99)
{
// summon regular dragon
}
if (DragonType = 100)
{
if (GoldDragonSpawn = true)
{
// summon gold dragon
}
}
}
}
}
}
這是有效的,因為 DragonTracker 是對象父級中的腳本。 如果不是這種情況,則 GetComponentInParent().GoldDragonInit = GoldDragonSpawn; 會像這樣被替換:
[SerializeField]
private GameObject DragonAncors;
void Start()
{
DragonAncors.GetComponent<DragonTracker>().GoldDragonInit = GoldDragonSpawn;
}
那里有很多錯誤,他們可能需要采取一些步驟來完成,但首先您應該清除您嘗試使用的代碼不受支持的問題。 進入項目設置並更改編譯器語言版本,因為它記錄了第 5 個錯誤。 這應該允許您使用較新的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.