- Home /
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.
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;}
}
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
Follow this Question
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