访问另一个对象上的脚本统一c#

时间:2014-12-18 11:42:56

标签: c# unity3d

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)
{

}


}

我正在尝试从另一个对象访问一个脚本,所以最终我可以检查一个通电阵列中对象的碰撞。

我得到一个未设置为实例的null异常对象。如果结尝试不同的解决方案,我已经束缚自己。我把它带回了骨头,仍然有问题。我将把gamemaster脚本放在下面,以便您可以看到完整的图片。

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);
    }
}

}

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,通过使public我想在碰撞上调整静态,然后通过我已经破坏了两个碰撞项目的脚本访问它。

我已经删除了powerupmaster,因为它不再需要了。

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");
}
  }