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 Makiavel · Dec 04, 2012 at 02:59 AM · c#3dgrid

3d grid of game objects - C# errors

 using UnityEngine;
 using System.Collections;
 
 
 public class Seed_Platforms_New : MonoBehaviour 
 {
     public float Xvalue = 0.0;
     public float Yvalue = 0.0;
     public float Zvalue = 0.0;
     private int Xcounter = 0;
     private int Ycounter = 0;
     private int Zcounter = 0;
     public int Xlimit = 10;
     public int Ylimit = 4;
     public int Zlimit = 20;
     public float ChangeAmount = 20.0;
     public GameObject PlatformRandomizer;
     public GameObject LadderTop;
     public GameObject LadderBot;
     public GameObject BridgeRandomizer;
     public GameObject ObjectToInstantiate;
     private float LADDER;
     private int SPECIAL_INST = 0;
     private bool EvenRow = false;
 
     void Update ()
     {
         if(Zcounter < Zlimit) 
         {
             while (Xcounter<Xlimit) 
             {
                 while (Ycounter<Ylimit) 
                 {
                     InstantiateTheObject();
                     Ycounter++ 
                     Yvalue =- (ChangeAmount/2);
                 }
                 Xcounter++; 
                 Xvalue =+ ChangeAmount;
                 ResetY();
             }
         ResetX();
         ResetY();
         Zvalue =+ (ChangeAmount / 2);
         Zcounter++
         }
 
         if(Zcounter < Zlimit) 
         {    
             Xvalue = (ChangeAmount / 2);
             while (Xcounter<Xlimit) 
             {
                 while (Ycounter<Ylimit) 
                 {
                     InstantiateTheObject();
                     Ycounter++ 
                     Yvalue =- (ChangeAmount/2);
                 }
                 Xcounter++; 
                 Xvalue =+ ChangeAmount;
                 ResetY();
             }
             ResetX();
             ResetY();
             Zvalue =+ (ChangeAmount / 2);
             Zcounter++
         }
 
     }
 
 public void InstantiateTheObject()
 {Instantiate (ObjectToInstantiate, Vector3(Xvalue, Yvalue, Zvalue), Quaternion.identity);}
 
 public void ResetX()
 {Xcounter = 0.0;
 Xvalue = 0.0;}
 
 public void ResetY()
 {Ycounter = 0.0;
 Yvalue = 0.0;}
 
 }

This code is supposed to instantiate a 3d grid of gameobjects, sorted "checkmate" style. It worked fine in javascript, but when I tried to rewrite it to the C#(this code), I got strange errors:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement to the first Ycounter++

And and unexpected } symbol at the closing point of the first (Zcounter < Zlimit).

What could cause these? I suspect that the errors are incredibly stupid, since this is one of the first C# scripts I wrote, but I would appreciate the help.

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

Answer by MarkFinn · Dec 04, 2012 at 05:32 AM

As you suspected, all minor issues. The first batch were just missing semi-colons after the increment commands. Then you were assigning doubles to floats (0.0 is a double by default. 0.0f is a float), and finally assigning some doubles to int variables (again 0.0 is a double, while 0 is an int.

Fixed code below.

No promises that the logic is right, but the commands are ok now.

 using UnityEngine;
 using System.Collections;
 
 
 public class Seed_Platforms_New : MonoBehaviour 
 {
     public float Xvalue = 0.0f;
     public float Yvalue = 0.0f;
     public float Zvalue = 0.0f;
     private int Xcounter = 0;
     private int Ycounter = 0;
     private int Zcounter = 0;
     public int Xlimit = 10;
     public int Ylimit = 4;
     public int Zlimit = 20;
     public float ChangeAmount = 20.0f;
     public GameObject PlatformRandomizer;
     public GameObject LadderTop;
     public GameObject LadderBot;
     public GameObject BridgeRandomizer;
     public GameObject ObjectToInstantiate;
     private float LADDER;
     private int SPECIAL_INST = 0;
     private bool EvenRow = false;
 
     void Update ()
     {
        if(Zcounter < Zlimit) 
        {
          while (Xcounter<Xlimit) 
          {
           while (Ycounter<Ylimit) 
           {
               InstantiateTheObject();
               Ycounter++ ;
               Yvalue =- (ChangeAmount/2);
           }
           Xcounter++; 
           Xvalue =+ ChangeAmount;
           ResetY();
          }
        ResetX();
        ResetY();
        Zvalue =+ (ChangeAmount / 2);
        Zcounter++;
        }
 
        if(Zcounter < Zlimit) 
        { 
          Xvalue = (ChangeAmount / 2);
          while (Xcounter<Xlimit) 
          {
           while (Ycounter<Ylimit) 
           {
               InstantiateTheObject();
               Ycounter++ ;
               Yvalue =- (ChangeAmount/2);
           }
           Xcounter++; 
           Xvalue =+ ChangeAmount;
           ResetY();
          }
          ResetX();
          ResetY();
          Zvalue =+ (ChangeAmount / 2);
          Zcounter++;
        }
 
     }
 
 public void InstantiateTheObject()
 {Instantiate (ObjectToInstantiate, new Vector3(Xvalue, Yvalue, Zvalue), Quaternion.identity);}
 
 public void ResetX()
 {Xcounter = 0;
 Xvalue = 0;}
 
 public void ResetY()
 {Ycounter = 0;
 Yvalue = 0;}
 
 }
Comment
Add comment · Show 1 · 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 Makiavel · Dec 04, 2012 at 08:37 AM 0
Share

Thank you for the fixes. They corrected the few actual mistakes, but the rest turned out to be some sort of glitch of the monobehaviour. I just created a new script, copy-pasted there the fixed content of this one and the weird errors were gone.

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

10 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

Related Questions

Having trouble with grid movement 1 Answer

Distribute terrain in zones 3 Answers

How to move character in a grid-based area? 1 Answer

Creating an Interactable Grid for Tactics Game (C#) 1 Answer

3D level generator on a grid 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