Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TempestInATeacup · May 22, 2015 at 07:02 PM · c#gameobjectinputactive

Activating GameObjects one by one

Hi, I'm trying to come up with a script that will activate one of two cubes upon the player clicking. so at first there are no cubes visible, then you click and one (lilCube) becomes visible, then you click again and the second (bigCube) becomes visible. The code I have right activates both instantly, with only one click, even though I've tried adding a variable for how many times you've clicked. I'm not sure what would be an efficient way to check if the first cube is active yet, and if not, activate it, and then if it is, activate the second. Here is my code:

 public GameObject lilCube;
 public GameObject bigCube;

 int clickTimes = 0;

 void Start ()
 {
     lilCube.SetActive(false);
     bigCube.SetActive (false);
 }

 void Update()
 {
     if (Input.GetButton ("Fire1") && lilCube.activeInHierarchy == false && clickTimes == 0) 
     {
         lilCube.SetActive (true);
         clickTimes = 1;
      }

     if (Input.GetButton ("Fire1") && lilCube.activeInHierarchy == true && clickTimes == 1)
     {
         bigCube.SetActive (true);
         clickTimes = 2;
     }
 }


Also, if I get this working, what would be an efficient way to do it for 10 cubes or more without making a cube prefab? Prefabs won't work for what I'm using this script for. Would I just have to keep doing more and more complicated if statements? there has to be a better way. I'm new to coding. sorry.

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
0
Best Answer

Answer by pako · May 23, 2015 at 06:48 AM

The following script works for any number of cubes you want, as long as you know that number up front. If you don't know up front the number of cubes, you can use ArrayList with some tweaking.

 using UnityEngine;
 
 public class ActivateCube : MonoBehaviour {
 
     //Create an array to hold any number of cubes you want
     public GameObject[] cubeArray = new GameObject[10]; //initialize the array for e.g. 10 cubes
 
     //Holds the index of the cubeArray, which corresponds to the next cube to be activated
     private int activateNext = 0;
 
 
     // Use this for initialization
     void Start () {
 
         for (int i = 0; i < cubeArray.Length; i++)
         {
             cubeArray[i].SetActive(false);
         }
     
     }
     
     // Update is called once per frame
     void Update () {
 
         //whenever a click occurs, and as long as activateNext is less than the length of the array...
         if (Input.GetButtonDown("Fire1") && activateNext < cubeArray.Length)
         {
             //... activate next cube
             cubeArray[activateNext].SetActive(true);
 
             activateNext++;
         }
     
     }
 }

 
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 TempestInATeacup · May 26, 2015 at 02:02 PM 0
Share

This is fantastic!! thanks! I didn't know how to make an array lol but this works great.

avatar image
0

Answer by Snawws · May 22, 2015 at 07:37 PM

I think the problem is that you dont use "else if" instead of "if" in the second check. The code is executed line by line. So you code right now first check if the amount of clicks is 0 then when that is true you se the amount of clicks to be 1, the directly after u check if the amount of clicks is 1 and this is now ture because you just set it to be.

So simply change the second if statement to "else if".

Comment
Add comment · Show 3 · 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 TempestInATeacup · May 22, 2015 at 07:58 PM 0
Share

This didn't change anything. I now have:

 if (lilCube.activeInHierarchy == true) 
     {
         if (Input.GetButton ("Fire1") && clickTimes == 1) 
         {
             bigCube.SetActive (true);
             clickTimes = 1;
         }
     }

     else if (lilCube.activeInHierarchy == false) 
     {
         if (Input.GetButton ("Fire1") && clickTimes < 1) 
         {
             lilCube.SetActive (true);
             clickTimes = 1;
         }
     } 

and it still does the same thing

avatar image Snawws · May 22, 2015 at 08:38 PM 1
Share

Use GetButtonDown ins$$anonymous$$d of GetButton. GetButton is for dragging and is called every update when the mouse button is down. GetButtonDown is only called once and is not repeted until it is click again. This happens so fast that it feels instant. I hope this is the answere to you problem. :)

avatar image pako · May 23, 2015 at 06:49 AM 0
Share

@Snawws nice catch, +1

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Check if the child of an object is active not working!!! 2 Answers

Distribute terrain in zones 3 Answers

how to know if GameObject is active without getting warning? 2 Answers

Script only works onetime 0 Answers

Code Not Working 1 Answer


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