作者rede1420 (rede1420)
看板GameDesign
标题[请益] unity是否可满足不同scrips的两个条件?
时间Thu Nov 30 22:53:25 2017
对不起我又来了
这次想实现的是一个若车子闯了红灯就会被扣分的设置
以下是设置碰撞後会扣分数的程式码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public int score = 100;
public Text scoreText;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
scoreText.text = ((int)score).ToString();
}
void OnTriggerEnter(Collider aaa) //aaa为自定义碰撞事件
{
if (aaa.gameObject.name == "Boom") //如果aaa碰撞事件的物件名称是
CubeA
{
score -= 32;
}
}
}
这边是红绿灯设置的程式码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tflc : MonoBehaviour
{
public Light Red;
public Light Green;
public Light Yellow;
// Use this for initialization
void Start()
{
StartCoroutine(Example());
}
IEnumerator Example()
{
Yellow.enabled = false;
while (true)
{
Green.enabled = true;
Red.enabled = false;
yield return new WaitForSeconds(10);
Yellow.enabled = true;
Green.enabled = false;
yield return new WaitForSeconds(4);
Red.enabled = true;
Yellow.enabled = false;
yield return new WaitForSeconds(10);
}
// Update is called once per frame
}
}
我想要实现车子在红灯是造成碰撞时才会被扣32分
之前有找到一个写法如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public int score = 100;
public Text scoreText;
public Tflc game;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
scoreText.text = ((int)score).ToString();
}
void OnTriggerEnter(Collider aaa) //aaa为自定义碰撞事件
{
if (game.Red.enabled == true || (aaa.gameObject.name == "Boom"))
{
score -= 32;
}
}
}
但是他不会扣分
想请教大家unity有办法让碰撞事件同时满足这两个条件吗
要怎麽改写才能实现我的需求
谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.134.69.177
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/GameDesign/M.1512053607.A.0C7.html
1F:推 BSpowerx: 你先确认到底有没有进OnTriggerEnter吧 11/30 23:38
2F:→ rede1420: 目前测试过了车子受到碰撞确实会扣32分, 11/30 23:41
3F:→ rede1420: 但是加了红绿灯的条件之後就不会扣分了 11/30 23:42
4F:→ joseph33: 有个东西叫Debug.log 还有个东西叫breakpoint 都很好用, 12/01 00:12
5F:→ joseph33: 你不试试吗? 12/01 00:12
6F:推 eugenelinrmx: 进入判断式if之前用Debug.log检查那两条件的状态 12/01 01:02
7F:推 amsmsk: 把||改成&&? 12/01 09:51
8F:推 wallissars: 你的写法只有碰撞Boom成立的时候才会成立 12/01 10:26
9F:推 wix3000: 可以把CODE移到HackMD之类的地方吗 用PTT我真的懒得看 12/01 12:41
10F:推 laikyo: collider 可以开关喔! 12/01 12:48
11F:推 tsrn46336686: 没有用到的function可以删掉吗XD (start) 12/01 19:06
12F:推 juicefish: Score脚本上有设定Tflc game是用谁吗 12/04 00:25
13F:→ juicefish: 我猜是game == null 然後直接game.Red null reference 12/04 00:26