- Home /
Help with Instantiating
I am having issues with my procedural city generation script. It is not complete yet, but I want to test it out in the editor, but I can't because I either get compiler errors or it will plain and simple not spawn anything at all. My first issue was that it would not let me redefine Number in any function. I resolved this issue by just making the script run a duplicate script on an instantiated object and rerun it like that, but no instantiated object will appear now. (yes, I kept in mind the random.range may not have returned as 1, so I set Number to 1 and it still did nothing.) The error I am getting is UnityException: get_transform is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'SecondCityGen'. See "Script Serialization" page in the Unity Manual for further details. SecondCityGen..ctor () (at Assets/SecondCityGen.js:9) My code is in the start function and I am not even using get_transform in any part of my code, so i am very puzzled. Could someone please explain to me what is wrong and possibly explain why variables can't be redefined?
Bonus: Who can explain to me how I can create a procedural generated city with my assets? I don't want to pay anything for a city generator asset either.
Here's my code:
#pragma strict
public var Crossroad: GameObject;
public var Road: GameObject;
var CityLimitX1 = 500;
var CityLimitZ1 = 500;
var CityLimitX2 = -500;
var cityLimitZ2 = -500;
var i = 0;
var CrossroadNX = transform.position.x + 30;
var TurnNX = transform.position.x + 60;
var RoadNX = transform.position.x + 30;
var Cross3wayNX = transform.position.x + 25;
var CrossroadNZ = transform.position.z + 30;
var TurnNZ = transform.position.z + 60;
var RoadNZ = transform.position.z + 20;
var Cross3wayNZ = transform.position.z + 30;
var Number = Random.Range(1, 16);
//static var CoordX =
//}
function Start () {
for (var i: int = 0; i < 1; i++) {
if (Number == 1) {
if (CrossroadNX < CityLimitX1) {
if (CrossroadNZ < CityLimitX1) {
Instantiate(Crossroad, new Vector3(i * 30.0F, 0, 0), Quaternion.Euler(Vector3(-90, 90, 0)));
Instantiate(Road, new Vector3(i * 30.0F, 0, 0), Quaternion.Euler(Vector3(-90, 90, 0)));
}
}
}
if (Number == 2) {
Instantiate(Crossroad, new Vector3(i * 30.0F, 0, 0), Quaternion.Euler(Vector3(-90, 90, 0)));
}
}
}
You need to do all of the following assignments in start:
var CrossroadNX = transform.position.x + 30; var TurnNX = transform.position.x + 60; var RoadNX = transform.position.x + 30; var Cross3wayNX = transform.position.x + 25; var CrossroadNZ = transform.position.z + 30; var TurnNZ = transform.position.z + 60; var RoadNZ = transform.position.z + 20; var Cross3wayNZ = transform.position.z + 30;
Your answer
![](https://koobas.hobune.stream/wayback/20220612124953im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What is typed variable? 2 Answers
Unity3d Javascript Reference Script Variable 1 Answer
Walking Sound Script not working properly (JavaScript) 1 Answer
Gun Script Help 2 Answers
Screenshot to variable 1 Answer