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 /
avatar image
0
Question by Miaw666 · Oct 27, 2019 at 03:16 PM · setactiveswitchingswitch objects

Switching between items after being picked up?

Hello! I'm making a game where you can collect "letter" pages. The player has a Clipboard in which he can view the collected pages. I made a code that worked exactly how I wanted, though it only worked when there were only 2 of these pages. I want to have a lot more pages, but if I did wth the code I'm using it will be a lot longer, when I just add one more page. So having around 10 pages would be a lot harder than it should. I'm new to making games, and this is the first code I've ever written, here it is:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PageFinder : MonoBehaviour { public GameObject page1; public GameObject page2;

 // Update is called once per frame
 void Update()
 {

     if (Input.GetKeyDown(KeyCode.Alpha1) && Page2Trigger.isPicked == true && Page1Trigger.isPicked == true)
     {
         page1.SetActive(true);
         page2.SetActive(false);
     }

     if (Input.GetKeyDown(KeyCode.Alpha2) && Page2Trigger.isPicked == true && Page1Trigger.isPicked == true)
     {
         page1.SetActive(false);
         page2.SetActive(true);
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Zaeran · Oct 27, 2019 at 03:40 PM

Store your pages in a List, then you can access them with a direct index.

I.e.

 public List<GameObject>() pages;
 //Disable all pages
 foreach(GameObject g in pages) {
     g.SetActive(false);
 }
 //Set page 5 active
 pages[4].SetActive(true);
Comment
Add comment · Show 2 · 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 Miaw666 · Oct 27, 2019 at 03:56 PM 0
Share

How do I use this?

I put the "foreach and pages[4] in the update. But how do I make a button that disables them after I pick them up? So that they won't get disabled before I can pick them up.

Do I add that to the code or replace it with something? Sorry, I'm new and don't really know a lot yet.

avatar image Zaeran Miaw666 · Oct 27, 2019 at 04:27 PM 1
Share

No worries :)

What you can do is only add them to the list once they've been picked up. So create a method like this:

 public void PickupPage(GameObject page)
 {
     if(!pages.Contains(page))
     {
         pages.Add(page)
     }
 }

Now your foreach loop will only affect the pages you've picked up, ins$$anonymous$$d of all if them. Just call that method each time a new page is picked up

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

113 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

Related Questions

Flickering when switching between GameObjects 0 Answers

physics.OverlapSphere colliders 1 Answer

Entering/Exiting Vehicle 90% complete 1 Answer

How to change scenes by touching objects? 2 Answers

Swappable clothing and armor help 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