Unity3d GameObject 调用其它GameObject中的方法

写了一个小小的东西,就只有三个类,Player,Enemy ,Bullet

当时有点小问题,不知道GameObject间怎么通信,现在写下来

在Enemy destroyr 的时候 调用了Player里面的类

我通过

 GameObject obj=GameObject.FindWithTag("Player");

_player=obj.gameObject.GetComponent();

来拿到Player的


using UnityEngine;

using System.Collections;


public class Player : MonoBehaviour {


public float moveSpeed=1f;

public GameObject bullet;

public AudioSource fireSound;

public Enemy enemy;

//private Enemy[] enemys;

private ArrayList enemys;

void Start () {

gameObject.name="Player";

enemys=new ArrayList();

for (int i=0;i<10;i++)

{

enemys.Add(Instantiate(enemy));

}

}

void OnTriggerEnter( Collider collision ){

if(collision.gameObject.name=="Enemy")

Destroy(gameObject);

}

public void UpdataEnemy(){

enemys.Add(Instantiate(enemy));

}

// Update is called once per frame

void Update () {

float moveDiff=Input.GetAxis("Horizontal")*moveSpeed*Time.deltaTime;

transform.Translate(Vector3.right*moveDiff);

if(gameObject.transform.position.x<-88.0f)

{

transform.position=new Vector3(88.0f,transform.position.y,transform.position.z);

//gameObject.transform.Translate(Vector3.right*88);

}else if(gameObject.transform.position.x>88.0f)

{

transform.position=new Vector3(-88.0f,transform.position.y,transform.position.z);

//gameObject.transform.Translate(Vector3.right*-88);

}

if(Input.GetKeyDown("space"))

{

Instantiate(bullet,new Vector3(this.transform.position.x,this.transform.position.y,this.transform.position.z) ,Quaternion.identity);

//fireSound.Play();

}

}

}



using UnityEngine;

using System.Collections;


public class Enemy : MonoBehaviour {

public float maxSpeed=100f;

public float minSpeed=5f;

private float currentSpeed;

private Player _player;

// Use this for initialization

void Start () {

gameObject.name="Enemy";

currentSpeed=Random.Range(maxSpeed,minSpeed);

float scale=Random.Range(5,10);

this.transform.localScale=new Vector3(scale,scale,scale);

this.transform.position=new Vector3(Random.Range(-77f,77f),Random.Range(10f,100f),11);

}

void Awake () {

       GameObject obj=GameObject.FindWithTag("Player");

_player=obj.gameObject.GetComponent();

    }

// Update is called once per frame

void Update () {

gameObject.transform.Translate(Vector3.down*currentSpeed*Time.deltaTime);

if(gameObject.transform.position.y<-30f)

{

destroy();

}

}

void OnTriggerEnter( Collider collision ){

if(collision.gameObject.name=="Bullet")

destroy();

}

private void destroy()

{

Destroy(gameObject);

if(_player)

_player.UpdataEnemy();

}

}


using UnityEngine;

using System.Collections;


public class Bullet : MonoBehaviour {


// Use this for initialization

void Start () {

gameObject.name="Bullet";

}

// Update is called once per frame

void Update () {

this.transform.Translate(Vector3.up*100*Time.deltaTime);

//Debug.Log("Bullet position===>"+this.transform.position.x+" "+this.transform.position.y+" "+this.transform.position.z);

if(this.transform.position.y>70.0f)

{

Destroy(this.gameObject);

}

}

void OnTriggerEnter( Collider collision ){

//Debug.Log(collision+"   Bullet collision"+collision.gameObject.name);

if(collision.gameObject.name=="Enemy")

Destroy(gameObject);

}

}




你可能感兴趣的:(unity)