Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Shadowskull1247 · Jun 27, 2017 at 04:23 AM · javascriptvariableinstantiationprocedural generationissues

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)));
 }
 
 }
 }
 
 
 
Comment
Add comment · Show 2
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 hexagonius · Jun 27, 2017 at 05:45 AM 3
Share

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;
avatar image Shadowskull1247 hexagonius · Jul 07, 2017 at 01:38 AM 0
Share

Thanks, I'm a retard. XD

0 Replies

· Add your reply
  • Sort: 

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

109 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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