unity破坏敌人后给Player加分
unity3d吧
全部回复
仅看楼主
level 12
部分相关代码如下:
public class LittleEnemy01 : MonoBehaviour {
2016年10月20日 10点10分 1
level 12
public class LittleEnemy01 : MonoBehaviour {
public GameObject Player;//定义Player
void Update () {
if (life <= 0) {
Destroy (this.gameObject);//
Player.GetComponent<PlayerData> ().PlayerScore += 5;
Player.GetComponent<PlayerData> ().PlayerXP += 2;
}
}
2016年10月20日 10点10分 2
level 12
public class LittleEnemy01 : MonoBehaviour {
public GameObject Player;//定义Player
public int life = 10;//敌人life为10
void Update () {
if (life <= 0) {
Destroy (this.gameObject);//life<=0则破坏敌人
Player.GetComponent<PlayerData> ().PlayerScore += 5;//Player得分加5
Player.GetComponent<PlayerData> ().PlayerXP += 2;//Player经验值加2
}
}
这个脚本添加在敌人上。我在inspector栏中也将Player指定为玩家,可实际上,破坏敌人后,玩家的得分和经验值都没有增加。难道
public GameObject Player;
...
Player.GetComponent<PlayerData> ().PlayerScore += 5;
这个用法不对吗?
2016年10月20日 10点10分 3
level 12
d
2016年10月20日 10点10分 4
level 12
c
2016年10月20日 10点10分 5
level 3
你把Destroy放在后面
2016年10月20日 11点10分 6
把player.换成GameObject.find就没问题,但听说find效率低
2016年10月20日 12点10分
@心乙夕口✨ Player 拖进去了吗? ,Player身上有 PlayerData 组件吗?
2016年10月20日 12点10分
没用
2016年10月20日 12点10分
回复
卧底0213
:[滑稽]全部都做了
2016年10月20日 12点10分
level 12
自顶
2016年10月20日 13点10分 7
level 8
在主角类定义两个方法,敌人类调用方法,传入参数
2016年10月20日 13点10分 8
这文字……[汗]
2016年10月20日 14点10分
level 10
[阴险]我又来撕逼了
2016年10月23日 09点10分 9
level 10
把destory放在加分的后面试试看, 对象都销毁了,怎么可能执行后面的。
2016年10月23日 09点10分 10
实际不是这样的
2016年10月23日 10点10分
回复 用户敏已被占用 :[阴险]用单例模式
2016年10月23日 10点10分
@苏紫夜丶 什么是单例模式?
2016年10月23日 11点10分
回复 用户敏已被占用 :就省了get组件了,你可以百度一下
2016年10月23日 11点10分
1