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
0
Question by kaliban · May 22, 2013 at 06:54 PM · c#menustatic

C# static problem. Making menu. Need advice, how to customize the game.

I'm very new to unity and to C#. The code I wrote works well, but I have difficulties in customizing it. I would like to make main menu, where gamer could choose not only statically assigned difficulties(like 2x2 or 4x4)but also to choose his own (for example 1x5 or 3x9) C# says that i & j must be static. But I cant understand why.

 using UnityEngine;
 using System.Collections;
 
 public class start : MonoBehaviour {
     public GameObject cube1;
     public GameObject sphere1;
     public  int counter=0;
     public static int j=30;
     public static int i=30;
     
 
     public int[,] table = new int[i, j];
     
     public GameObject[,] grid = new GameObject[i,j];
     
     
     void Start () {
     GenField();
     }
     void Update () {
     }
     
     void OnGUI() {
         GUI.Label(new Rect (10,10,150,20), "Turns: "+counter); 
     //    if(win){
     //         GUI.Label (new Rect (Screen.width/2-50, Screen.height/2-25, 150, 20), "You Win"); 
     //    }
     }
     
     
     
     void GenField(){
     float x1;
     float y1;
             for (int a = 0; a <= i-1; a++) {
                 for (int b = 0; b <= j-1; b++) {
                     table[a, b] = Random.Range(0, 2);
                 
                 x1=(float)a;
                 y1=(float)b;
                 if(table[a,b]==0){
                 grid[a,b] = Instantiate(cube1) as GameObject ;
                 grid[a,b].transform.position= new  Vector3(x1, y1);
                 grid[a,b].name="obj"+a+""+b;
                 } else {
                 grid[a,b] = Instantiate(sphere1) as GameObject ;
                 grid[a,b].transform.position= new  Vector3(x1, y1);
                 grid[a,b].name="obj"+a+""+b;
                 }
                 
             }
         }
     }
     
     
     public void ReceiveID(string name){
         //Debug.Log("received: "+name);
         
         
         for (int a = 0; a <= i-1; a++) {
                 for (int b = 0; b <= j-1; b++) {
                 
         if(grid[a,b].name==name){
                     
                     Inverser(a,b);
                     
                     CrossInverse(a,b);
                     
                     FieldCheckDestroy();
                 }
             }
         }
     }
     void CrossInverse(int a, int b){
         
         for(int z=0;z<=i-1;z++){
             if(z!=a){
                 Inverser(z,b);
             }
         }
         
         for(int z=0;z<=j-1;z++){
             if(z!=b){
                 Inverser(a,z);
             }
         }
         
     }
     void Inverser(int a, int b){
     float x1;
     float y1;
                 
                 x1=(float)a;
                 y1=(float)b;
                 
                     Destroy(grid[a,b]);
                     
                     if(table[a,b]==0){
                         grid[a,b] = Instantiate(sphere1) as GameObject ;
                         grid[a,b].transform.position= new  Vector3(x1, y1);
                         grid[a,b].name="obj"+a+""+b;
                         
                         table[a,b]=1;
                     }else {
                         grid[a,b] = Instantiate(cube1) as GameObject ;
                         grid[a,b].transform.position= new  Vector3(x1, y1);
                         grid[a,b].name="obj"+a+""+b;
                         table[a,b]=0;
                     }
                     
     }
     void FieldCheckDestroy(){
         bool win = true;
         for(int d=0;d<=i-1;d++){
             for(int f=0;f<=j-1;f++)
                 if(table[d,f]==0) win=false;
         }
         
         if(win){
             for(int d=0;d<=i-1;d++){
             for(int f=0;f<=j-1;f++){
                     Destroy(grid[d,f]);
                     
                 }
             }
             
             
         }
         
     }
 }
 
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 Tomer-Barkan · May 22, 2013 at 06:59 PM

It needs to be static only if you instantiate the array (give it value) in the class definition.

If you instantiate it within the code (for example in the Start() method) then you can use variables:

 public int[,] table = null;

 void Start () {
     table = new int[i, j]; // i,j don't need to be static now
     GenField();
 }
Comment
Add comment · Show 4 · 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 kaliban · May 22, 2013 at 07:00 PM 0
Share

But in this case array will not be visible for other functions...

avatar image Tomer-Barkan · May 22, 2013 at 07:14 PM 0
Share

Of course it will! It is still a class member, it's just set to null at first, and then in the Start() method it is set to receive an empty array.

Try it!

avatar image bigbat · May 22, 2013 at 07:15 PM 0
Share

@ kaliban

thus start function execute before any other funcs (like update function) therefore if you initialize your table in start other function can see your array.

avatar image Hoeloe · May 23, 2013 at 04:46 PM 0
Share

You don't even need to instantiate it in the first instance. Just writing public int[,] table; will implicitly instantiate it as null. And to kaliban, when working out the scope of a variable, you have to look at where it is declared, not where it is defined. In this case, table is declared in the class definition, but defined in Start. This means it will be visible to the entire class, but will be null until Start runs.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem with Pause Menu because of WaitForSeconds 1 Answer

How do you bind a c# model's property to the menu 1 Answer

Zelda like pause menu 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