• 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 Kristov · Nov 07, 2010 at 03:39 AM · gameobject

2 quick questions about GameObject[] SOLVED

Hi all,

I can't find reference to GameObject[] in any manual, so I'm asking my most basic questions here. I've made a script for customising hats, and have made each hat a value of var hats : GameObject[] . Here's the script:

var hats : GameObject[]; var activeHat : int;

function Update () { if (PlayerPrefs.GetInt("ChosenHat") > /I'm not sure here/) { PlayerPrefs.SetInt("ChosenHat", 0); } hats[activeHat].SetActiveRecursively(true); hats[!activeHat].SetActiveRecursively(false); }

My first question is: How do I access the 'size' part of the hats variable (i.e the max number of hats) and the second arises from the hats[!active].SetActiveRecursively(false); line. This works, however only when hats[activeHat] is 0 or 1. My 0 hat is just an empty game object, so the character has no hat, and the 1 hat is a cap. Whenever I flick the activehat variable between 0 and 1, it deactivates the cap and (I assume) activates the empty game object, and vice versa. However, when I make activeHat 2 or more, it activates the hats fine, but just doesn't deactivate any. So my second question is How do you get that working?

Thanks a bunch!

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

Answer by Eric5h5 · Nov 07, 2010 at 05:43 AM

The size of the array is hats.Length. I don't know what you're trying to do with "!activeHat"; the ! means not, which only works with true or false (1 or 0). You definitely don't want to use PlayerPrefs every frame in Update. It's for storing info on disk, so only call PlayerPrefs once when necessary.

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 Kristov · Nov 07, 2010 at 10:18 AM 0
Share

Right, awesome thanks for that. I was trying to make something saying every hat that's not currently on, and therefore not equal to the activeHat value, should be deactivated. Guess I done goofed with that though :P

ANd with the PlayerPrefs I was (With another selection script in my main menu) storing which hat was active in PlayerPrefs (Along with custom colors and such) so that the player doesn't have to set their hats or colors again in any scene, nor when you close the game and restart it. Perhaps there's a better way to do that too though? Buuuuut I bet it's more complicated :D

avatar image Eric5h5 · Nov 07, 2010 at 01:19 PM 0
Share

You should set the currently active hat to false first, then set the new active hat to true. If the hat data is persistent, then storing the info with PlayerPrefs is fine, but as I mentioned, accessing it every frame is a bad idea. Just do it once when necessary. Also setting the hats active/inactive every frame is not efficient as well; just do that once when necessary too. (i.e., don't use Update for those things.)

avatar image Kristov · Nov 07, 2010 at 10:47 PM 0
Share

Great thanks for your help. So basically, it'd be best to code something saying 'If the activeHat value hasn't changed since the last frame, do nothing, but if it has, do the script I have up there.'? I get the theory there, but I'd have no idea how to script it... As you can tell, my knowledge of scripting has a few gaping holes in it

avatar image Eric5h5 · Nov 07, 2010 at 11:47 PM 0
Share

Not quite; there's no need to check the activeHat value every frame. Just once when the user clicks the button or whatever.

avatar image Kristov · Nov 08, 2010 at 01:18 AM 0
Share

Right, yeah if I attach the two scripts together I could do that... $$anonymous$$ind of blatantly obvious solution there :S Thanks again for all your help!

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

No one has followed this question yet.

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

enabling / disabling child objects 1 Answer

DontDestory gameobject doesn't work on button click after a new scene loads 2 Answers

Unity Detonator Problems 1 Answer

OnDestroy collision detect 4 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