Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 hellojbb1234 · Oct 04, 2016 at 05:11 AM · c#gameobjectactive

How do I fix my code so that these If statements turn an object off or on depending on a number

I have the code down below able to produce a random number but it only turn one object on each time it picks the same object each time no matter the number.

 using UnityEngine;
 using System.Collections;
 
 public class RandomAudio : MonoBehaviour
 {
     public GameObject One;
     public GameObject Two;
     public int Fun;
     // Use this for initialization
     void Start()
     {
         
               
 
 
         Fun = Random.Range(1,3);
 
     
       
     }
     public void StartMusic()
     {
         if (Fun == 1)
         {
             One.gameObject.SetActive(true);
             Two.gameObject.SetActive(false);
 
 
         }
        if(Fun == 2)
         {
             Two.gameObject.SetActive(true);
             One.gameObject.SetActive(false);
         }
     }
 
     // Update is called once per frame
     }
 
 


Comment
Add comment · Show 2
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 ScaniX · Oct 04, 2016 at 03:44 PM 0
Share

Could you add a Debug.Log() in the Start() method to print out the value of Fun and in Start$$anonymous$$usic() to see the ti$$anonymous$$g of those methods? Is Start() called before Start$$anonymous$$usic() (e.g. is Start$$anonymous$$usic() called from some Awake() method?).

What is the value of Fun in the inspector (as it is public)?

avatar image NoseKills · Oct 04, 2016 at 05:24 PM 0
Share

Are you calling Start$$anonymous$$usic() somewhere?

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by hellojbb1234 · Oct 04, 2016 at 10:02 PM

Right You guys aren't to helpful with this type of stuff you overcomplicated it. Just do this next time

 using UnityEngine;
 using System.Collections;
 
 public class RandomAudio : MonoBehaviour {
     public float NumberSelect;
     public GameObject One;
     public GameObject Two;
     // Use this for initialization
     void Start() {
         NumberSelect = Random.Range(1, 3);
 
     }
 
     // Update is called once per frame
     void Update() {
         if (NumberSelect <= 1) 
         { 
         One.gameObject.SetActive(true);
 
         }
         else
         {
             Two.gameObject.SetActive(true);
         }
 
     }
 
 }
 
     

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 doublemax · Oct 04, 2016 at 10:35 PM 1
Share

This code is logically different from the original one (e.g. no gameobject is ever de-activated). And the original problem wasn't really solved, you just used a different method without understanding what was wrong in the old code.

But if you're happy with that, so be it.

avatar image
0

Answer by doublemax · Oct 04, 2016 at 05:25 AM

Start() is only called once. Move the "Fun = Random.Range(1,3);" into the StartMusic() method.

Comment
Add comment · Show 5 · 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 hellojbb1234 · Oct 04, 2016 at 03:28 PM 0
Share

the problem is something like a logic error and any number that is picked always causes it to pick the second gameobject and I don't why. Also I only need the number picked once right now maybe later when the song ends and I need to cycle through the different songs

avatar image mj321 hellojbb1234 · Oct 04, 2016 at 03:35 PM 0
Share

Under the same conditions the random number generator will always produce the same sequence.

Try adding this line at the beginning of Start():

 Random.InitState( Time.time );
avatar image hellojbb1234 mj321 · Oct 04, 2016 at 04:55 PM 0
Share

Ok that is an int but Time.time is a float so it wont convert and anyway the thing is I got it to generate either one or two which is what I want, it is the if statements that dont seem to work right as in if its one it still uses the second gameobject. EDIT Also if I uncheck the objects in unity then neither object will become active

Show more comments

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

how do i check if a gameobject is not active? 0 Answers

"Field " " is never assigned to, and will always have it's default value null" 2 Answers

How do i convert game object counts, into int, and put into string? 0 Answers

Spawning stops after second scene load 2 Answers

OnMouseOver wont work with fps Controller 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