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 johnalabama · Sep 14, 2013 at 04:42 PM · c#instantiateprefabrandom

Can't set the random position for an Instance.

Hello everyone :)

I'm trying to do my own whack-a-mole kind of game. I made an array of Vector3s with possible positions and randomising them to Debug.log works fine. I have an error though, when I try to use the random position when instantiating my prefab. Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class pojawia_w_miejscu : MonoBehaviour {
 
     public GameObject zombiePrefab;
     
     pozycje pozycja = new pozycje();
     
     
     void Update ()
     {
         if(Input.GetKeyDown(KeyCode.A))
         {
             Object zombieInstance;
             zombieInstance = Instantiate(zombiePrefab, pozycja.positions[Random.Range(0, 8)]);
             
         }
     }
 }


And here's an error: No overload for method 'Instantiate' takes '2' arguments Pozycje is the class that has an array storing possible positions.

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 vexe · Sep 14, 2013 at 04:51 PM 0
Share

What in the world is a pozycje and pozycja? :D

avatar image johnalabama · Sep 14, 2013 at 11:08 PM 0
Share

accordingly positions and position in my little known language :) cheers!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by vexe · Sep 14, 2013 at 04:48 PM

Just check the docs, Instantiate takes an Object, position and rotation:

static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

you're only giving the first two:

 zombieInstance = Instantiate(zombiePrefab, pozycja.positions[Random.Range(0, 8)]);

So try:

 zombieInstance = (GameObject) Instantiate(zombiePrefab, pozycja.positions[Random.Range(0, 8)], Quaternion.identity);

A side note: when you're dealing with arrays/lists + Random.Range, let the max range be always array.Length-1 or list.Count-1 so that you don't accidentally go beyond your array/list limit and get Index out of bounds exception.

Comment
Add comment · Show 4 · 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 vexe · Sep 14, 2013 at 05:38 PM 0
Share

How did it go? did my answer help you? if so, please mark it as correct :)

avatar image johnalabama · Sep 14, 2013 at 11:06 PM 0
Share

had to give up my machine for a moment for this: http://vimeo.com/67897675 :) it'll be back after the techno party in couple hours and I bet your answear's gonna make it happen :) thanks!

avatar image johnalabama · Sep 15, 2013 at 04:00 PM 0
Share

After tinkering with my random function and reading everything here http://docs.unity3d.com/Documentation/$$anonymous$$anual/InstantiatingPrefabs.html it works bonkers :)

Thanks!

avatar image vexe · Sep 15, 2013 at 04:02 PM 0
Share

Always welcome :)

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

16 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

Related Questions

How To NOT Instantiate the same Prefab Twice (C#) 1 Answer

instantiating vertically 2 Answers

instantiate random prefabs based on player camera distance 1 Answer

How to get all objects instantiated from a single prefab? 1 Answer

How to access property of a prefab before Instantiating. 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