• 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
1
Question by songie · Apr 15, 2012 at 02:52 PM · c#

.test is a field but a type was expected

Unity throws me 3 errors

1 test is a field but a type was expected 2 the best overloaded method match for ".label has some invalid arguments 3 cannot convert object experssion to type string

I am trying to get data back from my datbase itterating trough all the values stored inside.

     public string test = "";
 public string testing = "";
 public int AtrribCounter = 0;

private IEnumerator CountMaxAttributesUsed() {

     var bucket = new Bucket("Database Attribute & Vital Setup");
     
     var bucketData = new BucketData {Bucket = bucket};

             // Get a list of all keys in the bucket.
             var getKeysReq = bucket.GetKeys();
             yield return getKeysReq.WaitUntilDone();

                    // Build a new list of keys in the bucket.
                     bucketData.Entries = new List<Entry>();
                     
                     foreach (var key in getKeysReq.GetKeyEnumerable())
                     {    
                         //gets the number of total keys under said bucket
                         AtrribCounter++;
                     }
     //itterates trough each key and combines attribute string with the value
     for(int cnt = 0; cnt < AtrribCounter; cnt++) {
 
         var DataList = bucket.Get("Attribute " + cnt);
     
         yield return DataList.WaitUntilDone(); 
     
         Debug.Log("Wrote one value and got a reply. Success = " + DataList.isSuccessful); 
            
         test = DataList.GetValue<string>();
     }
 }

then under OnGUI my code that prints it on the screen is

for(int cnt = 0; cnt < AtrribCounter; cnt++) {

GUI.Label(new Rect(0, 90 + (cnt * 25), 125, 25), ("Attribute " + cnt));

 GUI.Label(new Rect(125, 90 + (cnt * 25), 125, 25), (test)cnt);
             }

the moment i add cnt after the (test) it throws the errors, if i leave cnt out i see it going trough all the correct values but then prints out only the last string multiple times.

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

Answer by aldonaletto · Apr 15, 2012 at 06:22 PM

Are you writing C# or JS? The first lines are C#, and they should be inside a class declaration like below. Then some JS instructions appear: the keyword var is used in JS variable declarations, variables are declared in C# with the type first, then the name. You then return to C# with a foreach instruction - it's just for in JS.
Definitely, you must choose one language to write your script!

using UnityEngine;

public class MyScriptName : MonoBehaviour { public string test = ""; public string testing = ""; public int AtrribCounter = 0;

private IEnumerator CountMaxAttributesUsed() { // this is JS! var bucket = new Bucket("Database Attribute & Vital Setup"); // it should be something like this in C#: Bucket bucket = new Bucket(...); ...

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 songie · Apr 15, 2012 at 08:25 PM 0
Share

I cut and pasted parts of the code as the entire script is too long, the code is in c# and uses some examples from Unity Park UgameDB, witch are also in C#.

the use both for and foreach and declare the buckets as var, if declare them as Bucket bucket = new Bucket(...); i cant store or retrieve data from them.

The entire script works, except i get the error by using the counter (cnt) there.

avatar image aldonaletto · Apr 15, 2012 at 08:39 PM 0
Share

Ok, it seems the script is pure C# - I googled around and found that var is also used in C# - don't know exactly how, but since it's already working, don't modify it!
If your problem is just the code ("Attribute " + cnt), use ("Attribute " + cnt.ToString()) - ToString returns the string representation of cnt, and should work fine.

avatar image
0

Answer by songie · Apr 16, 2012 at 12:43 AM

Its not actually the ("Attribute " + cnt), use ("Attribute " + cnt.ToString()) part that gives errors but the combination of getting a value from the database put into the string test then attaching cnt to count trough the keys and display them, without cnt to count trough them i see it rush trough them and then display the end result in all x GUI printouts on the screen, it stops working the moment i add cnt to it.

test = DataList.GetValue(); ->> GUI.Label(new Rect(125, 90 + (cnt 25), 125, 25), (test)cnt*); <-- the cnt part wont fly :/ and is needed to print out each value in correct order.

thanks for the help so far.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 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