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 /
This question was closed Jul 13, 2012 at 11:26 PM by TheDarkVoid for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TheDarkVoid · Jul 13, 2012 at 10:26 PM · c#arraysintkeycode

C# Problem with assigning values arrays

i have declared an array:

 private int[] ForbiddenKeys;

i have created a function that allows me to add values to the array:

 void AddForbidden(KeyCode key)
 {
  ForbiddenKeys[ForbiddenKeys.Length] = (int)key;
 }

and in the start function i call the function:

  AddForbidden(KeyCode.Escape);
  AddForbidden(KeyCode.CapsLock);
  AddForbidden(KeyCode.F1);
  AddForbidden(KeyCode.F2);
  AddForbidden(KeyCode.F3);
  AddForbidden(KeyCode.F4);
  AddForbidden(KeyCode.F5);
  AddForbidden(KeyCode.F6);
  AddForbidden(KeyCode.F7);
  AddForbidden(KeyCode.F8);
  AddForbidden(KeyCode.F9);
  AddForbidden(KeyCode.F10);
  AddForbidden(KeyCode.F12);

but unity tell me this:

 Assets/Scripts/Menu/Options/Options_Controls.cs(20,23): warning CS0649: Field `Options_Controls.ForbiddenKeys' is never assigned to, and will always have its default value `null'

can anyone help me with this, i have also tried manually assigning the values like this:

 ForbiddenKeys[0] = (int)KeyCode.Escape;
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

  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Jul 13, 2012 at 10:38 PM

You havent initialized the array to a length and you can't set elements unless the array is initialized. You should probably

    using System.Collections.Generic;

    ...
 
    private List<int> forbiddenKeys = new List<int>();

Then add them like this:

    forbiddenKeys.Add(KeyCode.F9);
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 Bunny83 · Jul 13, 2012 at 10:47 PM 0
Share

Exactly. An array is an object and therefore a reference type. You have to create an instance of that object to use it. Allso arrays can't change the size! The size have to be specified when you create the array. A List is the best solution in this case.

Also is there a reason why you want to use "int" ? Usually it's better to keep the enum type (which is of course also an int, but it's more typesafe that way).

 List<$$anonymous$$eyCode> forbidden$$anonymous$$eys = new List<$$anonymous$$eyCode>();
avatar image TheDarkVoid · Jul 13, 2012 at 11:25 PM 0
Share

thanks this worked

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Changing a GUI String to read as a INT 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using a Parameterized arraylist (C#)??? 1 Answer

Best way to keep track of objects on a 3D Grid? 2 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