• 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 Ghachii · Sep 28, 2011 at 12:26 AM · javascriptinstantiateprefabduplicateradius

Scripting a prefab to duplicate itself within a fixed radius

I'm trying to do something very simple with JavaScript, but cobbling together a script from the samples in the reference documentation isn't working for me at all.

I have a cylinder prefab, and all I want it to do is duplicate itself in a random position, within a certain radius. That cylinder would then also duplicate itself, etc, into infinity. Idealy, I'd also like to be able to adjust the number of duplicates that are instantiated.

I guess the first thing to do is to make the cylinder duplicate itself at all. This is all I have so far:

 var cylinder : GameObject;

function Update () {

 yield WaitForSeconds (3);
 cylinder = Instantiate(transform.position, transform.rotation);

}

Basically, I don't understand the syntax for 'Instantiate'.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by save · Sep 28, 2011 at 12:37 AM

Here's how you can work with instantiation:

 var myPrefab : GameObject;
 Instantiate(myPrefab, Vector3.zero, Quaternion.identity);

In the script above we instantiate a predefined object which is set to the variable myPrefab in the Inspector. The position is 0 on world-XYZ and the rotation is unchanged.

 var myPrefab : GameObject;
 var myGameObject = Instantiate(myPrefab, Vector3.zero, Quaternion.identity);

In this script we reference the prefab instantiation to myGameObject. The object will still instantiate as normal but will be able to be easily modified directly and referenced to. For instance:

 myGameObject.name = "Instantiated Prefab";

You always define like this: Instantiate(the object to spawn, the position, the rotation);

To instantiate something at a random position try this out:

 var rndX : int = Random.Range(-10, 10);
 var rndY : int = Random.Range(-10, 10);
 var rndZ : int = Random.Range(-10, 10);
 Instantiate(myPrefab, transform.position+Vector3(rndX, rndY, rndZ), Quaternion.identity);

Keep in mind that transform.position means the scripts current transform's position in world-space.

Comment
Add comment · 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
0

Answer by syclamoth · Sep 28, 2011 at 12:42 AM

Working version of your code, because explaining it is harder than demonstrating-

var circleRadius : float; var waitTime : float;

function Start() { StartCoroutine(WaitAndDuplicate()); }

function WaitAndDuplicate() { while(true) { yield WaitForSeconds(waitTime); var randomOffset : Vector3 = Random.insideUnitCircle * circleRadius; randomOffset = Vector3(randomOffset.x, randomOffset.z, randomOffset.y); Instantiate(gameObject, transform.position + randomOffset, transform.rotation); } }

Add this script to the gameObject you want turned into a serious grey-goo scenario, and watch your GPU melt!

Instantiate takes 3 things- an original, a location, and a rotation. It copies the original, and puts it where you tell it to with the other two parts! As for the circle thing, Random.insideUnitCircle is a nice way of making random offsets in 2D- I had to rotate it back to the normal plane (in which x and z are the main coordinates and y is height, a decision I may never understand), so that it would spread out instead of up and down.

If you want your object to spawn anywhere in space (not just in a circle), replace

Random.insideUnitCircle

with

Random.insideUnitSphere

still multiplying this with the desired maximum radius (so that it is not just a number between Vector3.zero and one in any direction).

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

How can I instantiate more prefabs as the score increases? 2 Answers

After instantiating an instance of a prefab and changing the value of a variable in a script attached to the prefab, that value is lost 1 Answer

Cannot assign prefab in Inspector? 2 Answers

Prefab Spawning 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges