C# 访问另一个对象unity c上的脚本
我正在尝试从另一个对象访问脚本,以便最终可以在一系列通电中检查对象的冲突 我得到一个未设置为实例的空异常对象。如果有人尝试不同的解决方案,我会把自己绑在一起。我已经把它带回了赤裸裸的骨头,仍然有这个问题。我将把gamemaster脚本放在下面,这样你就可以看到完整的画面C# 访问另一个对象unity c上的脚本,c#,unity3d,C#,Unity3d,我正在尝试从另一个对象访问脚本,以便最终可以在一系列通电中检查对象的冲突 我得到一个未设置为实例的空异常对象。如果有人尝试不同的解决方案,我会把自己绑在一起。我已经把它带回了赤裸裸的骨头,仍然有这个问题。我将把gamemaster脚本放在下面,这样你就可以看到完整的画面 using UnityEngine; using System.Collections; public class PowerUpsMaster : MonoBehaviour { public GameObject pla
using UnityEngine;
using System.Collections;
public class PowerUpsMaster : MonoBehaviour {
public GameObject player;
private GameMaster playcontrol;
public GameObject mushroom;
public GameObject skull;
public GameObject mask;
public GameObject dragoneye;
//GameObject [] powerUps;
void Awake ()
{playcontrol = player.GetComponent<GameMaster>();
}
void Start ()
{
if (playcontrol != null)
{
playcontrol.powerUps [0] = mask;
playcontrol.powerUps [1] = dragoneye;
playcontrol.powerUps [2] = skull;
playcontrol.powerUps [3] = mushroom;
}
Debug.Log ("powerup is" + playcontrol.powerUps[0]); **ERROR IS HERE*****
Debug.Log ("powerup is" + playcontrol.powerUps[1]);
Debug.Log ("powerup is" + playcontrol.powerUps[2]);
Debug.Log ("powerup is" + playcontrol.powerUps[3]);
}
void OnTriggerEnter2D(Collider2D other)
{
}
}
我已经解决了这个问题,将我想在冲突时调整的公共int设置为静态,然后通过我已经拥有的脚本访问它,该脚本破坏了两个冲突项 我已经删除了powerupmaster,因为它不再需要了
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameMaster : MonoBehaviour
{
public GameObject player;
public GameObject[] enemies;
public GameObject[] powerUps;
public GameObject[] coins;
public Vector3 spawnValues;
public Vector3 powerUpValues;
//public Vector3 playerStartpos;
// not needed same as powerupvalues public Vector3 coinValues;
public int enemyCount;
public int powerUpCount;
public int coinCount;
public float spawnWait;
public float startWait;
public float waveWait;
void Awake ()
{
if (player == null)
player = GameObject.FindGameObjectWithTag ("Player");
Vector3 playerStartpos= new Vector3 (-12,0,0);
Instantiate (player,playerStartpos,Quaternion.identity);
// ,playerStartpos,Quaternion.identity);
}
void Start ()
{
StartCoroutine (SpawnWaves ());
StartCoroutine (SpawnPowerUps ());
StartCoroutine (SpawnCoins ());
}
IEnumerator SpawnPowerUps ()
{
yield return new WaitForSeconds (startWait);
while (true) {
for (int i = 0; i < powerUpCount; i++) {
if (powerUps == null)
powerUps = GameObject.FindGameObjectsWithTag ("PowerUps");
GameObject powerUp = powerUps [Random.Range (0, powerUps.Length)];
Vector3 spawnPosition = new Vector3 (Random.Range (-powerUpValues.x,
powerUpValues.x), Random.Range (-powerUpValues.y, powerUpValues.y), spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (powerUp, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
IEnumerator SpawnCoins ()
{
yield return new WaitForSeconds (startWait);
while (true) {
for (int i = 0; i < coinCount; i++) {
if (coins == null)
coins = GameObject.FindGameObjectsWithTag ("Coin");
GameObject coin = coins [Random.Range (0, coins.Length)];
Vector3 spawnPosition1 = new Vector3 (Random.Range (-powerUpValues.x,
powerUpValues.x), Random.Range (-powerUpValues.y, powerUpValues.y), spawnValues.z);
Quaternion spawnRotation1 = Quaternion.identity;
Instantiate (coin, spawnPosition1, spawnRotation1);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < enemyCount; i++)
{
if (enemies == null)
enemies = GameObject.FindGameObjectsWithTag("Enemy");
GameObject enemy = enemies[Random.Range(0, enemies.Length)];
Vector3 spawnPosition2 = new Vector3 (spawnValues.x,Random.Range (-spawnValues.y,
spawnValues.y), spawnValues.z);
Quaternion spawnRotation2 = Quaternion.identity;
Instantiate (enemy, spawnPosition2, spawnRotation2) ;
/*yield return new WaitForSeconds (spawnWait);
Vector3 spawnPosition1 = new Vector3 (spawnValues.x,Random.Range (-spawnValues.y,
spawnValues.y), spawnValues.z);
Quaternion spawnRotation1 = Quaternion.Euler (0,180,0);
Instantiate (crow, spawnPosition1, spawnRotation1) ;
yield return new WaitForSeconds (spawnWait);
Vector3 spawnPosition2 = new Vector3 (spawnValues.x,Random.Range (-spawnValues.y,
spawnValues.y), spawnValues.z);
Quaternion spawnRotation2 = Quaternion.Euler (0,180,0);
Instantiate (goldenEagle, spawnPosition2, spawnRotation2) ;
yield return new WaitForSeconds (spawnWait);
Vector3 spawnPosition3 = new Vector3 (spawnValues.x,Random.Range (-spawnValues.y,
spawnValues.y), spawnValues.z);
Quaternion spawnRotation3 = Quaternion.Euler (0,180,0);
Instantiate (baldEagle, spawnPosition3, spawnRotation3) ;
*/
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
}
粘贴错误并向我们显示相关代码,其中显示的错误是[unity3d]标记而不是网站此部分的[unity]标记:unity3d只是网站地址,unity是引擎的名称,这也是一个2d游戏。NullReferenceException:对象引用未设置为对象Powerupmaster的实例。从Assets/Scripts/Powerupmaster开始。cs:28这是Powerupmaster中的调试行,我已编辑并用星号标记。谢谢。我对自己的答案一点也不满意,但代码暂时有效,所以我可以继续下一节。我打算回到这里,一旦我找到了如何在正确的脚本中访问的方法,就使用public而不是static。请回答并告诉我正确的方法:-
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DestroyByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject smoke;
//public AudioMaster audioCall;
public AudioClip audioCoinCollect;
public AudioClip audioExplosion;
private SpriteRenderer spriteRenderer;
//private ScoreManager scoreManager;
void OnTriggerEnter2D(Collider2D other)
{
if (this.gameObject.tag == "Player" && other.tag == "Enemy")
{
//AudioMaster.
//audio.PlayOneShot (audioExplosion);
Instantiate (explosion, other.transform.position, other.transform.rotation);
Destroy (other.gameObject);
Destroy (gameObject);
}
if (this.gameObject.tag == "PlayerWeapon" && other.tag == "Enemy")
{
audio.PlayOneShot (audioExplosion);
Instantiate (explosion, other.transform.position, other.transform.rotation);
Destroy (other.gameObject);
Destroy (gameObject);
ScoreMaster.score += 100;
}
if (this.gameObject.tag == "Enemy" && other.tag == "Player")
{
audio.PlayOneShot (audioExplosion);
Instantiate (explosion, other.transform.position, other.transform.rotation);
Instantiate (explosion, this.transform.position, this.transform.rotation);//change this to
different explosion for player
Destroy (other.gameObject);
Destroy (gameObject);
}
if (this.gameObject.tag == "Player" && other.tag == "PowerUps")
{
if (other.gameObject.name == "dragonmask(Clone)")
{
PlayerController.speed = 1;
}
else if (other.name == "powerupdragoneye(Clone)")
{
PlayerController.speed = 8;
}
else if (other.gameObject.name == "powerupskull(Clone)")
{
PlayerController.speed = 25;
}
else if (other.gameObject.name == "powerupmushroom(Clone)")
{
PlayerController.speed = 50;
}
audio.PlayOneShot (audioCoinCollect);
Instantiate (smoke, other.transform.position, other.transform.rotation); //may be changed
to points later.
Destroy (other.gameObject);
Debug.Log("Power Up Type "+ other.gameObject.name);
// GameObject.Find("GameController").GetComponent("GameMaster");
// if (other.gameObject = mushroom)
// {
//
// }
//will add gem collected or powerup to player here.
//Destroy (gameObject);
}
if (this.gameObject.tag == "Player" && other.tag == "Coin")
{
if (other.gameObject.name == "coin4(Clone)")
{
CoinMaster.coins += 1;
}
else if (other.name == "purplegem(Clone)")
{
CoinMaster.coins += 5;
}
else if (other.gameObject.name == "redgem(Clone)")
{
CoinMaster.coins += 4;
}
else if (other.gameObject.name == "bluegem(Clone)")
{
CoinMaster.coins += 3;
}
audio.PlayOneShot (audioCoinCollect);
Instantiate (smoke, other.transform.position, other.transform.rotation); //may be changed
to points later.
Destroy (other.gameObject);
Debug.Log("coin type "+ other.gameObject.name);
//will add coins to coin score
//Destroy (gameObject);
}
////(explosion, transform.position, transform.rotation);
//if (other.tag == "Player")
///{
// Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
//(explosion, other.transform.position, other.transform.rotation);
//gameController.GameOver ();
//}
//Destroy(other.gameObject);
//Destroy(gameObject);
//Debug.Log ("Explosion should happen");
}
}