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
1
Question by metal_invader · Oct 28, 2012 at 08:49 PM · instantiatevariablespacing

Spacing objects out when instantiated using variables.

I had trouble with a script earlier that was supposed to spawn an increasing amount of objects. Now the script is almost finished but now I'm having a different problem. From the documentation on instantiating objects there was a method in there that spaced out the objects. namely this:

 for(var w : int = 0; w < 10; w++)
         {
         Instantiate(prefab, Vector3(w * 2, 0, 0), Quaternion.identity);
         }


now in my code I have replaced vector 3 with a variable but unity doesn't seem to like this.

 var wave_number = 0;
 var wave_text : GUIText;
 var wave_amount = 5;
 var background_timer = 0;
 var wave : boolean;
 var prefab : Transform;
 var spawnpoint : Transform;
 private var spawnpoint_location : Vector3;
 
 function Start () {
 }
 
 function Update () {
 background_timer = Time.time % 11;
     if(background_timer == 10)
     {
     wave = true;
     Invoke("New_Wave", 1);
     }
 }
 
 function New_Wave () {
     if(wave)
     {
     spawnpoint_location = spawnpoint.position;
     wave_number++;
     wave_amount += 5;
     Debug.Log("Wave number is " + wave_number.ToString() + ". " + "There are " + wave_amount.ToString() + " enemies.");
     wave = false;
     wave_text.text = ("Wave " + wave_number.ToString() );
         for(var w : int = 0; w < wave_amount; w++)
         {
         Instantiate(prefab, spawnpoint_location(w * 2, 0, w * 2), Quaternion.identity);
         }
     }
 
 }

With the above code I get this error:

Assets/Scripts/wave_spawner.js(35,37): BCE0077: It is not possible to invoke an expression of type 'UnityEngine.Vector3'.

And removing the part in the loop that spawns the NPCs in a spaced fashion gets rid of the error, but when they instantiate they are all on top of each other.

My question is, how can I space the objects out while keeping the location as a variable (with which I can place a GameObject in to manually set a spawn point)?

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 aldonaletto · Oct 28, 2012 at 08:57 PM

Calculate spawnpoint_location each iteration, like this:

    for(var w : int = 0; w < wave_amount; w++)
    {
    spawnpoint_location = spawnpoint.position + Vector3(w * 2, 0, w * 2); 
    Instantiate(prefab, spawnpoint_location, Quaternion.identity);
    }
Comment
Add comment · Show 3 · 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 metal_invader · Oct 28, 2012 at 09:11 PM 0
Share

Yep. That took care of the error. Though I wonder why unity doesn't like that I try to space them out in the instantiate function?

avatar image aldonaletto · Oct 28, 2012 at 09:57 PM 0
Share

You could calculate the position directly in the Instantiate function call as well:

 Instantiate(prefab, spawnpoint.position + Vector3(w * 2, 0, w * 2), Quaternion.identity);

The problem with your original code was generated by this expression:

 spawnpoint_location(w * 2, 0, w * 2)

This looks to the compiler as a call to a function referenced by spawnpoint_location.

avatar image metal_invader · Oct 28, 2012 at 10:03 PM 0
Share

Ooooh I see. Thank you for explaining.

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

Best way to pass variables between instantiated/colliding objects? 1 Answer

Using a top level variable in a class 0 Answers

setting the text of an instantiated prefab's child's guitext object 1 Answer

Changing game object based on variable state? 1 Answer

Spawn Random Enemy 3 Answers


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