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
-1
Question by Wuzseen · Jul 27, 2012 at 04:15 AM · c#methodnew

Expression denotes a `type', where a `variable', `value' or `method group' was expected

Hi all, I've seen a couple questions with this error. But I am not sure what exactly is going on. I'm pretty sure I have "news" in all the right spots but it just doesn't want to take it.

I'm using a 2D array (a 9x9 grid) to store a spawn points in a grid for a prefab. I also need to keep track if something is on in the grid, thus I made a custom class:

 public class gridSpawn {
  GameObject prefab;
  public bool on;
  Vector3 pos;
 
  public gridSpawn( GameObject n, Vector3 b ){
   prefab = n;
   pos = b;
  }
 
  bool toggleOn(){ // toggles on, returns if val of on
   on = !on;
   return on;
  }
 
  void spawn(){
    GameObject.Instantiate(prefab, pos, Quaternion.identity);
  }
 }

I'm setting up the grid in my start function in the main class for the script:

 public class SpawnGrid : MonoBehaviour {
  public GameObject pre = new GameObject();
  private gridSpawn[,] grid;
  // Use this for initialization
  void Start () {
  grid = new gridSpawn[9,9];
  for( int i = 0; i < 9; i++ )
  {
  for( int z = 0; z < 9; z++ )
  gridSpawn[i][z] = new gridSpawn( pre, new Vector3( i * 20 - 90    , z * -20 + 90, 80 ) );
   }
  }
 
  // Update is called once per frame
  void Update () {
  
  }
 }

The line in question is gridSpawn[i][z], I just don't see what I'm missing. The news are there, but it seems to suggest that pre somehow needs to be "new" yet it's a member of the class...

Anyway. I apologize for the formatting, it's durdling hard.

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 Kryptos · Jul 27, 2012 at 08:12 AM

You use a multidimensional array, but you are trying to access it as a jagged array.

Either change the declaration, or the way you access it:

 /// multidimensional array
 // declaration
 private gridSpawn[,] grid;
 // initialization
 grid = new gridSpawn[9,9];
 // usage
 grid [i,j] = something;

 /// jagged array
 // declaration
 private gridSpawn[][] grid;
 // initialization
 grid = new gridSpawn[9][];
 for(int i=0; i<9; i++)
 {
     grid[i] = new int[9];
 }
 // usage
 grid [i][j] = something;

Since you now excalty the size of your array, I would recommend the use of a multidimensional array which is faster and memory friendlier.

Note: about that last comment, multidimensional arrays should be faster than jagged array, but CLR implementation on Microsoft .Net is very bad. I don't know how this is implemented on Mono. But we can hope that this is better done.

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 Wuzseen · Jul 27, 2012 at 03:13 PM 0
Share

Thanks, I also just realized another error with the code to.

$$anonymous$$y class was named gridSpawn and I durdled with the line of code: gridSpawn[i,z] = new gridSpawn(...);

It should have been: grid[i,z] = new gridSpawn(...);

I ran into issues with the multi vs. jagged right afterwards, so thanks!

avatar image Kryptos · Jul 27, 2012 at 03:22 PM 0
Share

Yes and in fact the error message was related to this error anyway. I should have been more careful.

As a general rule, you should start the name of your class with an Uppercase. This is not mandatory but Unity and .Net API do follow this rule (the only exception are for basic types such as bool or float, although there is a Uppercase version for each).

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using WaitForSeconds and trigger 1 Answer

very funny problems,a function doesn't excute to the end 2 Answers

Saved MethodInfo variable resets on compile 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