简体   繁体   English

在另一个对象上访问脚本统一C#

[英]accessing a script on another object unity c#

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

}


}

I am trying to access a script from another object so eventually I can check for collision of an object in a array of powerups. 我试图从另一个对象访问脚本,所以最终我可以检查上电阵列中某个对象的冲突。

I am getting a null exception object not set to an instance. 我收到未设置为实例的空异常对象。 I have tied myself if knots trying different solutions. 如果遇到不同的解决方案,我会束手无策。 I have brought it back to bare bones and still have the issue. 我已将其恢复原状,但仍然存在问题。 I will put the gamemaster script below so you can see the full picture. 我将把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);
    }
}

}

I have solved this by making the public int I wanted to adjust on collision a static and then accessed it via a script I already had that destroyed the two colliding items. 我已通过将要在碰撞时调整的public int设为静态,然后通过我已经销毁了两个碰撞项的脚本来访问它来解决此问题。

I have removed powerupmaster as it is no longer needed. 我已经删除了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");
}
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM