【发布时间】:2018-07-07 12:15:39
【问题描述】:
编辑
所以它就在这里,我对此毫无疑问,但为什么呢? NextOne.active = true;无论是否按下操作按钮,都会触发。
if(Input.GetButtonDown("Action") && Op_3 == true)
{
AudioSource.PlayClipAtPoint(OptionsSound, transform.position);
Playerhealthscript.panic +=1;
print("1 works");
Option_3_SELECTED.active = false;
Option_4_Unselected.active = false;
Option_3_Unselected.active = false;
Option_4_SELECTED.active = false;
NextOne.active = true;
}
else if(Input.GetButtonDown("Action") && Op_4 == true)
{
AudioSource.PlayClipAtPoint(OptionsSound, transform.position);
Playerhealthscript.insanity +=1;
print("2 works");
Option_3_SELECTED.active = false;
Option_4_Unselected.active = false;
Option_3_Unselected.active = false;
Option_4_SELECTED.active = false;
NextOne.active = true;
}
按照我的阅读方式,如果按下操作按钮并且 Opt 3 或 Opt 4 处于活动状态,那么应该会发生以下情况:
播放音频片段 在 Playerhealth 脚本中,Insanity OR Panic 增加 1 在控制台中打印一条消息 选项 3 和 4 被禁用 NextOne 被激活。
那么我在这里没有看到什么?为什么 NextOne 在按下或不按下操作的情况下激活?我的语法错了吗?
结束编辑
让我告诉你我的脚本应该做什么:
出现一些文字。出现一个带有 A/B 选项的问题。您选择 A 或 B。这会激活一个上面有类似脚本的 GameObject。该脚本会立即禁用您只是其中之一的脚本。你回答另一个 A/B 问题。 (这次来自新脚本,因为旧脚本已被停用)一旦您回答了第二个问题(新脚本上只有一个),第三个 GameOBject 将再次激活另一个类似的脚本,结果相同。这是一条链子。所以基本上它去了 GameObject 1 在场景开始时处于活动状态。 (Script1) 做出 A/B 选择 GameObject 1 被停用 GameObject 2 被激活(Script2) 做出新的 A/B 选择 GameObject3 被激活(Script3) 等等。 它应该是一个简单的开/关切换脚本,但这是发生了什么: 当玩家做出第一个决定而不是从 GameObject 1 转到 GameObject 2 时,脚本会绕过 2 并直接转到 3。这是从 1 到 2 的脚本:
//boolean variables
var Speaking : boolean = false;
var Entered : boolean = false;
var Speaking2 : boolean = false;
var Entered2 : boolean = false;
var Speaking3 : boolean = false;
var Entered3 : boolean = false;
var Speaking4 : boolean = false;
var Entered4 : boolean = false;
var Op_1 : boolean = false;
var Op_2 : boolean = false;
var Op_3 : boolean = false;
var Op_4 : boolean = false;
//speaking variables
var speakername : String;
var theselines : String;
var theselines2 : String;
var theselines3 : String;
var theselines4 : String;
var theselines5 : String;
var these2lines : String;
var these2lines2 : String;
var these2lines3 : String;
var these2lines4 : String;
var these2lines5 : String;
var these3lines : String;
var these3lines2 : String;
var these3lines3 : String;
var these3lines4 : String;
var these3lines5 : String;
var these4lines : String;
var these4lines2 : String;
var these4lines3 : String;
//other variables
var Background : GameObject;
var Option_1_Unselected : GameObject;
var Option_2_Unselected : GameObject;
var Option_1_SELECTED : GameObject;
var Option_2_SELECTED : GameObject;
var NextOne : GameObject;
var MouseLookScript : MouseLook;
public var guiSkin : GUISkin;
var OptionsSound : AudioClip;
var MoveSound : AudioClip;
var Playerhealthscript : Playerhealth;
function Start () {
Entered = true;
}
function Continued(){
yield WaitForSeconds(0.25);
Entered = false;
Speaking = false;
Entered2 = true;
}
function Continued2(){
yield WaitForSeconds(0.25);
Entered2 = false;
Speaking2 = false;
Entered3 = true;
}
function Continued3(){
yield WaitForSeconds(0.25);
Entered3 = false;
Speaking3 = false;
Entered4 = true;
}
function OnGUI(){
if(Entered){
Background.active = true;
MouseLookScript.enabled = false;
Speaking = true;
GUI.skin = guiSkin;
GUI.Label(Rect(410,295,400,175), speakername);
GUI.Label(Rect(500,345,400,175), theselines);
GUI.Label(Rect(500,380,400,175), theselines2);
GUI.Label(Rect(500,415,400,175), theselines3);
GUI.Label(Rect(500,455,400,175), theselines4);
GUI.Label(Rect(500,495,400,175), theselines5);
if(Input.GetButtonDown("Action")){
Continued();
}
}
if(Entered2){
Speaking2 = true;
GUI.skin = guiSkin;
GUI.Label(Rect(410,295,400,175), speakername);
GUI.Label(Rect(500,345,400,175), these2lines);
GUI.Label(Rect(500,380,400,175), these2lines2);
GUI.Label(Rect(500,415,400,175), these2lines3);
GUI.Label(Rect(500,455,400,175), these2lines4);
GUI.Label(Rect(500,495,400,175), these2lines5);
if(Input.GetButtonDown("Action")){
Continued2();
}
}
if(Entered3){
Speaking3 = true;
GUI.skin = guiSkin;
GUI.Label(Rect(410,295,400,175), speakername);
GUI.Label(Rect(500,345,400,175), these3lines);
GUI.Label(Rect(500,380,400,175), these3lines2);
GUI.Label(Rect(500,415,400,175), these3lines3);
GUI.Label(Rect(500,455,400,175), these3lines4);
GUI.Label(Rect(500,495,400,175), these3lines5);
if(Input.GetButtonDown("Action")){
Continued3();
}
}
//Question_1
if(Entered4)
{
Speaking4 = true;
GUI.skin = guiSkin;
GUI.Label(Rect(410,295,400,175), speakername);
GUI.Label(Rect(415,345,400,175), these4lines);
GUI.Label(Rect(415,380,400,175), these4lines2);
GUI.Label(Rect(415,415,400,175), these4lines3);
Option_1_SELECTED.active = true;
Option_2_Unselected.active = true;
Option_1_Unselected.active = false;
Option_2_SELECTED.active = false;
Op_1 = true;
Op_2 = false;
if(Input.GetAxis("Vertical") < -.5)
{
Option_1_SELECTED.active = false;
Option_2_Unselected.active = false;
Option_1_Unselected.active = true;
Option_2_SELECTED.active = true;
Op_1 = false;
Op_2 = true;
print("this works");
}
if(Input.GetButtonDown("Action") && Op_1 == true)
{
AudioSource.PlayClipAtPoint(OptionsSound, transform.position);
Playerhealthscript.panic +=1;
print("1 works");
Option_1_SELECTED.active = false;
Option_2_Unselected.active = false;
Option_1_Unselected.active = false;
Option_2_SELECTED.active = false;
NextOne.active = true;
}
else if(Input.GetButtonDown("Action") && Op_2 == true)
{
AudioSource.PlayClipAtPoint(OptionsSound, transform.position);
Playerhealthscript.insanity +=1;
print("2 works");
Option_1_SELECTED.active = false;
Option_2_Unselected.active = false;
Option_1_Unselected.active = false;
Option_2_SELECTED.active = false;
NextOne.active = true;
}
}
}
我认为这应该可以解决问题,所以我认为问题出在 2 到 3 的脚本中。下面是 2 到 3 的脚本。
#pragma strict
//boolean variables
var Speaking5 : boolean = false;
var Entered5 : boolean = false;
var Op_3 : boolean = false;
var Op_4 : boolean = false;
//speaking variables
var speakername : String;
var these5lines : String;
var these5lines2 : String;
var these5lines3 : String;
//other variables
var Background : GameObject;
var Option_3_Unselected : GameObject;
var Option_4_Unselected : GameObject;
var Option_3_SELECTED : GameObject;
var Option_4_SELECTED : GameObject;
var OldOne : GameObject;
var NextOne : GameObject;
var MouseLookScript : MouseLook;
public var guiSkin : GUISkin;
var OptionsSound : AudioClip;
var MoveSound : AudioClip;
var Playerhealthscript : Playerhealth;
function Start () {
OldOne.active = false;
NextOne.active = false;
Entered5 = true;
}
function OnGUI(){
if(Entered5)
{
Speaking5 = true;
GUI.skin = guiSkin;
GUI.Label(Rect(410,295,400,175), speakername);
GUI.Label(Rect(415,345,400,175), these5lines);
GUI.Label(Rect(415,380,400,175), these5lines2);
GUI.Label(Rect(415,415,400,175), these5lines3);
Option_3_SELECTED.active = true;
Option_4_Unselected.active = true;
Option_3_Unselected.active = false;
Option_4_SELECTED.active = false;
Op_3 = true;
Op_4 = false;
if(Input.GetAxis("Vertical") < -.5)
{
Option_3_SELECTED.active = false;
Option_4_Unselected.active = false;
Option_3_Unselected.active = true;
Option_4_SELECTED.active = true;
Op_3 = false;
Op_4 = true;
print("this works");
}
if(Input.GetButtonDown("Action") && Op_3 == true)
{
AudioSource.PlayClipAtPoint(OptionsSound, transform.position);
Playerhealthscript.panic +=1;
print("1 works");
Option_3_SELECTED.active = false;
Option_4_Unselected.active = false;
Option_3_Unselected.active = false;
Option_4_SELECTED.active = false;
NextOne.active = true;
}
else if(Input.GetButtonDown("Action") && Op_4 == true)
{
AudioSource.PlayClipAtPoint(OptionsSound, transform.position);
Playerhealthscript.insanity +=1;
print("2 works");
Option_3_SELECTED.active = false;
Option_4_Unselected.active = false;
Option_3_Unselected.active = false;
Option_4_SELECTED.active = false;
NextOne.active = true;
}
}
}
【问题讨论】:
-
我注意到如果我注释掉 NextOne.active = true;在脚本 2 中,问题被正确询问,但显然脚本 3 没有触发。所以我想知道它是否是位置。我已经移动了它,甚至将它放在自己的功能中,但没有成功。
-
停止使用 OnGui 并开始使用the new UI
-
好的,在我重新发明轮子之前,有什么关于如何修复这个脚本的建议吗?
-
我没有想法,抱歉。您的设置很复杂,应该重构为更优雅的对话系统。
-
您的代码格式毫无用处,而且该方法对我来说似乎过于复杂......但我的第一个猜测是:玩家点击的那一刻,您激活了脚本 2,它可能直接跳转到
OnGui虽然Input.GetButtonDown仍然是true-> 它直接进入脚本 3
标签: unity3d unityscript gameobject