- Home /
Wrong GameObject activates
I am at one of those moments in my code that pisses me off so much.
Let me tell you what it's supposed to do:
Some text appears. A question with an A/B choice appears. You choose A or B. This activates a GameObject which has a similar script on it. That script immediately disables the script you were just on. You answer another A/B question. (this time from the new script, since the old one has been deactivated) Once you answer the second question (only one on the new script) a third GameOBject activates, with another, similar script again, with the same results. It's a chain. So basically it goes
GameObject 1 is active at start of scene. (Script1) A/B choice is made GameObject 1 is deactivated GameObject 2 is activated(Script2) New A/B choice is made GameObject3 is activated(Script3) and so on.
It should be a simple on / off switch script, but here 's what's happening:
When the player makes the first decision instead of going from GameObject 1 to GameObject 2 the script is bypassing 2 and going straight to 3. Here is the script from 1 to 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;
}
}
}
That should do the trick I think, so I believe that the problem resides somewhere in the script for 2 to 3. I also checked my inspector in Unity several times to make certain that everything is up to snuff there, and it appears to be.
#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;
}
}
}
Now at this point you might be saying "Conect," this is stupid. Why are you daisy chaining a bunch of empty GameObjects with what amounts to the same script over and over again? That's actually a really good question. Originally, this was one big script, which I had asked about here: link text After days of pulling my hair out trying to figure out why question 1 was being asked, but not question 2 I decided to try and jerryrig the whole mess by breaking it up into 10 GameObjects with smaller, similar scripts that would activate and deactivate as they ran. It seemed so simple. The whole damn thing seems so simple, and my dumba$s can't figure it out. Help. Please and thank you.
I'm noticing that if I comment out NextOne.active = true; in script 2 the question gets asked properly, but then obviously script 3 doesn't fire off. So I'm wondering if it's placement. I've moved it around, even put it in its own function with no success.
So it's in here, of this I have no doubt, but why? NextOne.active = true; is firing off regardless of whether the action button is being pressed.
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;
}
}
}
The way that I'm reading this, if action button is pressed AND Opt 3 OR Opt 4 are active then the following should happen:
An audio clip is played Insanity OR Panic are increased by 1 in the Playerhealth script A message is printed in the console Options 3 & 4 are deactivated NextOne is activated.
So what am I not seeing here? Why is NextOne activating with or without action being pressed? Is my syntax wrong?
Answer by Conect11 · Jan 31, 2018 at 02:52 AM
Got it! My son pointed out that all I needed to do was add an integer every time a question was answered, then make that integer a condition to go on to the next question.
So my solution wasnt fast enough ;(. I’m glad you at least finished and got your script running.
I'm so very thankful for you trying to help. It had nothing to do with speed. $$anonymous$$y hope was to use Unity Answers to help me understand why my script wasn't working. I knew I was close, but couldn't put my finger on why. I knew I didn't have to reinvent the wheel, just find what was causing the hiccup.
Totally understandable but I’ll still continue til I finish the script so others can use it. $$anonymous$$aybe you could find use in it later on :D
@$$anonymous$$ittenSnipes absolutely! I think the more ways we can see how to do this, the better. Also, here's the script in working form, although there's one other little thing now that that is fixed that I'm going to post as a new question.
#pragma strict
//boolean variables
var Speaking5 : boolean = false;
var Entered5 : boolean = false;
var Op_3 : boolean = false;
var Op_4 : boolean = false;
var Question : int = 0;
//speaking variables
var speakername : String;
var addhealth : String;
var addhealth2 : 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 $$anonymous$$ouseLookScript : $$anonymous$$ouseLook;
public var guiSkin : GUISkin;
var OptionsSound : AudioClip;
var $$anonymous$$oveSound : AudioClip;
var Playerhealthscript : Playerhealth;
function Update(){
if(Question >= 1)
{
NextOne.active = true;
}
}
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");
Question += 1;
Option_3_SELECTED.active = false;
Option_4_Unselected.active = false;
Option_3_Unselected.active = false;
Option_4_SELECTED.active = false;
}
else if(Input.GetButtonDown("Action") && Op_4 == true)
{
AudioSource.PlayClipAtPoint(OptionsSound, transform.position);
Playerhealthscript.insanity +=1;
print("2 works");
Question += 1;
Option_3_SELECTED.active = false;
Option_4_Unselected.active = false;
Option_3_Unselected.active = false;
Option_4_SELECTED.active = false;
}
}
}
Answer by KittenSnipes · Jan 29, 2018 at 03:50 AM
I have been hard at work for this script and here is the results for now. I am going to continue to work on it but I wanted to at least show what I have done. So far it only works with 2 answers per question and it does not log the answer pressed. I will have more later.
Pokemon Mystery Dungeon Like Script Update:
[Header("Interview Questions: ")]
public List<Interview> InterviewQuestions;
[Header("GameObject References Of UI: ")]
public Text questionText;
public Canvas InterviewCanvas;
private int currentQuestion;
private bool buttonPressed;
[Header("Text Options: ")]
public Font TextFont;
public FontStyle StyleOfFont = FontStyle.Normal;
public float LineSpacing = 1;
public float offsetFromQuestionText = 35;
void Start()
{
//We go to the start of our question list
currentQuestion = 0;
//We set our font
questionText.font = TextFont;
//Set our font style
questionText.fontStyle = StyleOfFont;
//Set the correct line spacing
questionText.lineSpacing = LineSpacing;
//And say our button is not pressed
buttonPressed = false;
//Start our interview
StartCoroutine(WaitingForButtonPress());
}
IEnumerator WaitingForButtonPress()
{
if ((currentQuestion + 1) <= InterviewQuestions.Count)
{
if (InterviewQuestions[currentQuestion].answers.Count == 2)
{
//Set the question text
questionText.text = InterviewQuestions[currentQuestion].question;
//Instantiate the answers
GameObject firstAnswer = Instantiate(questionText.gameObject);
GameObject secondAnswer = Instantiate(questionText.gameObject);
//Do all of this to set the rotation,
//position, text for answer
//to both existing
//choosable answers.
firstAnswer.transform.SetParent(InterviewCanvas.transform);
firstAnswer.transform.position = new Vector3(questionText.transform.position.x,
questionText.transform.position.y - offsetFromQuestionText,
questionText.transform.position.z);
firstAnswer.transform.rotation = questionText.transform.rotation;
firstAnswer.name = InterviewQuestions[currentQuestion].answers[0];
secondAnswer.transform.position = new Vector3(firstAnswer.transform.position.x,
firstAnswer.transform.position.y - offsetFromQuestionText,
firstAnswer.transform.position.z);
secondAnswer.transform.rotation = firstAnswer.transform.rotation;
secondAnswer.transform.SetParent(InterviewCanvas.transform);
secondAnswer.name = InterviewQuestions[currentQuestion].answers[1];
//This is where the button component comes in
firstAnswer.AddComponent<Button>();
secondAnswer.AddComponent<Button>();
//We change our answer texts
firstAnswer.GetComponent<Text>().text = InterviewQuestions[currentQuestion].answers[0];
secondAnswer.GetComponent<Text>().text = InterviewQuestions[currentQuestion].answers[1];
//We listen for the click event and when it happens generate our next question
firstAnswer.GetComponent<Button>().onClick.AddListener(GenerateNextQuestion);
secondAnswer.GetComponent<Button>().onClick.AddListener(GenerateNextQuestion);
//Our button press is set to false because we already pressed it
buttonPressed = false;
}
}
else
{
//If all questions are answered our interview is over
Debug.LogError("Interview Over!");
//Put an event here I recommend using a seperate function
}
yield return new WaitUntil(() => buttonPressed == true);
}
void GenerateNextQuestion()
{
if (!(currentQuestion >= InterviewQuestions.Count - 1))
{
//Get the previous answers to the previous question
GameObject previousAnswer1 = GameObject.Find(InterviewQuestions[currentQuestion].answers[0]);
GameObject previousAnswer2 = GameObject.Find(InterviewQuestions[currentQuestion].answers[1]);
//Make them disappear
previousAnswer1.SetActive(false);
previousAnswer2.SetActive(false);
//Go to the next question
currentQuestion = currentQuestion + 1;
//Set the question text to the next question text
questionText.text = InterviewQuestions[currentQuestion].question;
//Our button has been pressed and our functions are done so that means
//our button has been pressed
buttonPressed = true;
}
else
{
//Get the previous answers to the previous question
GameObject previousAnswer1 = GameObject.Find(InterviewQuestions[currentQuestion].answers[0]);
GameObject previousAnswer2 = GameObject.Find(InterviewQuestions[currentQuestion].answers[1]);
//Make them disappear
previousAnswer1.SetActive(false);
previousAnswer2.SetActive(false);
//And make the question disappear
questionText.gameObject.SetActive(false);
Debug.LogError("Interview Over!");
//I am not sure which one of these worked lol so put an event
//on this one too.
}
//Start the interview for the next question
StartCoroutine(WaitingForButtonPress());
}
}
//This is our InterviewQuestions class
//[System.Serializable] is important
//Makes our class accessible to change.
[System.Serializable]
public class Interview
{
//Our question
public string question;
//List of our answers
public List<string> answers;
}
That is correct $$anonymous$$ittenSnipes, it s a (portion of) a character creation script. $$anonymous$$y last comment has where I've figured out where things go wrong, just not why. Also, I made a Youtube video showing what's happening. link text
Ok great well I am currently working on an easier way to do all of that. So once I am finished I would be glad to put the simpler scripts here.
you rock the house $$anonymous$$ittenSnipes. I really am trying to wrap my head around WHY this isn't working, because it seems so simple. What sucks is this is supposed to be my easier solution to what was initially one huge 822 line code that literally had the same problem.
Updated my answer. It is only a start but I will continue to work on it. Try it out and see if it works for you :D.
So now the only part you need to do is the algorithm or in better terms choose the outcome based off of the answers given by the player. If you want me to help, and I will gladly help, I need to know the outcomes you want and the questions and stuff. Otherwise I cant do anything. Just tell me your choice. After that I will put the updated script.
you are incredible, $$anonymous$$ittenSnipes. Will let you know further, but I also don't want you to have to put too much effort into this XD
I love doing these types of things is it isn’t really a big deal but I do need some form of event to happen after selecting all of the replies. Can you help me out these?
@$$anonymous$$ittenSnipes so the questions are all multiple choice with only two possible answers and include things like:
Your mum makes roast and potatoes. Which do you eat first?
What is 8 cut in half? 3 or 4?
Your father offers you a drink, do you accept it or no?
Your father in law offers you 10 drinks, do you drink with him yes or no?
You wake up alone in a field a hundred miles from home. Adventure or disaster?
The player health script contains stats for the player that each answer will add a point to. Does that help? I really appreciate this.
So no matter which choice they make they get a point to their health?
Well, it's not health as in hit points. In the health script there are sections for sanity, resourcefulness, diabetes, flu, cancer, and panic. Each answer deter$$anonymous$$es where a point is going to go.
Ok then I will make a basic thing to add. So I will make each answer hold a type and you will choose what to put there
Your answer
Follow this Question
Related Questions
How could I center a GUI box on screen? 3 Answers
Java Script error Q's MissingReferenceExeption BCE0034 and BCE005 0 Answers
How to hide/unhide a GUI image when character enters trigger. 1 Answer
[JS] Custom camera rotation resets on Var change? 0 Answers
Best way to emulate Swift String Interpolation in UnityScript? 1 Answer