• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Stoyanow · Apr 26, 2016 at 10:18 PM · c#unity 5easy

Is there an easier way to do this?

Hello,

I am making a scene with int 1 and int 2 as possible answers, to questions I've asked in a previous scene. Explanation of one of the answers gameobjects 1. Created the gameobject (that consists of UI images) with two possible answers - 1 and 2. They are integers. 2. Created 2 gameobjects (consits of UI image of a tick) and set them as un-active. Attached them to the parent gameobject(the answers).

If the player answers with the first answer he sets the playerprefs to interger 1. And if he answers with the second answer he sets the playerprefs to integer 2.

Here is the code, it's easier to understand form it:

 using UnityEngine;
 using System.Collections;
 
 public class History : MonoBehaviour {
 
 public GameObject t_1_A;
     public GameObject t_1_B; 
     public GameObject t_2_A;
     public GameObject t_2_B;
     public GameObject t_3_A;
     public GameObject t_3_B; // ----------> these are the answers(ticks)
     public GameObject t_4_A;
     public GameObject t_4_B;
     public GameObject t_5_A;
     public GameObject t_5_B;
 
     int question1;
     int question2;
     int question3; //from here we set int to  get the playerprefs
     int question4;
     int question5;
 
        void Start () {
         
         question1 = PlayerPrefs.GetInt("Question1");
         question2 = PlayerPrefs.GetInt("Question2");
         question3 = PlayerPrefs.GetInt("Question3"); // we get the playerprefs
         question4 = PlayerPrefs.GetInt("Question4");
         question5 = PlayerPrefs.GetInt("Question5");
 
 
              if(question1 == 1){
             t_1_A.SetActive(true);
             t_1_B.SetActive(false);
 
         }
         else if(question1 == 2){
             t_1_A.SetActive(false);
             t_1_B.SetActive(true);
         }
 
         if(question2 == 1){
             t_2_A.SetActive(true);
             t_2_B.SetActive(false);
         }
         else if(question2 == 2){
             t_2_A.SetActive(false);
             t_2_B.SetActive(true);
         }
         if(question3 == 1){
             t_3_A.SetActive(true);
             t_3_B.SetActive(false);
         }
         else if(question3 == 2){
             t_3_A.SetActive(false);
             t_3_B.SetActive(true);
         }
         if(question4 == 1){
             t_4_A.SetActive(true);
             t_4_B.SetActive(false);
         }
         else if(question4 == 2){
             t_4_A.SetActive(false);
             t_4_B.SetActive(true);
         }
         if(question5 == 1){
             t_5_A.SetActive(true);
             t_5_B.SetActive(false);
         }
         else if(question5 == 2){
             t_5_A.SetActive(false);
             t_5_B.SetActive(true);
         }
      }
  }



Now i have to add like 20-30 more answers to questions asked in the first scene of the game, and it gets messy. I would be grateful if some more skillful programmer helps me out here.

Thank you.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Pharaoh_ · Apr 26, 2016 at 10:49 PM

If there are always 2 answers to your questions, you can do this:

 public class History : MonoBehaviour {
 
     public GameObject[] answers;
     int[] questions;
 
     void Start () {
         int newLength = answers.Length/2;
         questions = new int [newLength];
         for (int i = 0; i < newLength; i++) {
             questions [i] = PlayerPrefs.GetInt ("Question" + i.ToString());
             if (questions [i] == 1 || questions [i] == 2) {
                 if (questions [i] == 1) {
                     answers [i].SetActive (true);
                     answers [i + 1].SetActive (false);
                 } else {
                     answers [i].SetActive (false);
                     answers [i + 1].SetActive (true);
                 }
             }
         }
     }
 }

Could potentially be optimized further with a mod(ulo).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stoyanow · Apr 26, 2016 at 11:47 PM 0
Share

Thanks a lot! I can fully understand the code, although I could never come up with it myself! What's weird is that it's supposed to work, and it does to some degree, but what happens is this: The first half of the answers have both the ticks activated, and the second half of the answers have none

alt text

alt text

2.jpg (74.7 kB)
1.jpg (47.7 kB)
avatar image
0

Answer by Stoyanow · Apr 28, 2016 at 10:10 AM

If anyone stumbles on this, I am still trying to fix the issue.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Objects are created in the same location. 0 Answers

RPC Instantiate issue, Prefabs incompatible with MAC ? 0 Answers

Trying to transform my character's local position on a GetKey. 0 Answers

How to toggle elements within a prefab in Unity 0 Answers

How do i use lines to calculate triangles on mesh?? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges