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 ROM · Jun 18, 2010 at 08:36 AM · 3diphonerandomclassclasses

Creating a class?

I have the following code to create objects at random locations (I still need to write the random code):

public GameObject objectPrefab;

void Start() { StartCoroutine( SpawnObjects() ); }

IEnumerator SpawnObjects() { while( true ) { Instantiate( objectPrefab, /however you need to determine a valid random position/, Quaternion.identity ); yield return new WaitForSeconds( 1.0f ); } }

Now to initiate it I apparently need to put the code in a class and attach it to an empty object (Thanks Tetrad for that!). But I was wondering how I could go about creating and using this class in the context of this problem?

Thanks in advance!

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
Best Answer

Answer by Mike 3 · Jun 18, 2010 at 08:50 AM

This should do it:

using UnityEngine; using System.Collections;

public class SameNameAsFilename : MonoBehaviour { public GameObject objectPrefab;

 void Start()
 {
     StartCoroutine( SpawnObjects() );
 }

 IEnumerator SpawnObjects()
 {
     while( true )
     {
         Instantiate( objectPrefab, /*however you need to determine a valid random position*/, Quaternion.identity );
         yield return new WaitForSeconds( 1.0f );
     }
 }

}

If it's the same name as the filename and is a monobehaviour, you'll be able to just drag it onto a gameobject in the scene

The two using statements are to make the unity and IEnumerator specific parts compile

Comment
Add comment · Show 1 · 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 ROM · Jun 18, 2010 at 09:15 AM 0
Share

Thanks! It works! I've just got one more problem....;)

avatar image
0

Answer by ROM · Jun 18, 2010 at 09:14 AM

It works! Thanks so much! Huzzah!

The only problem I'm having is it isn't creating one object per second like it should. Thousands of objects fly out until it crashes. I can only assume its because it keeps calling the script over and over again. I can't figure out what the problem is :/.

Heres the completed code:

using UnityEngine; using System.Collections;

public class Spawn : MonoBehaviour { private Vector3 tempVector;

 public GameObject objectPrefab;

 void Start()
 {
     StartCoroutine( SpawnObjects() );
 }

 IEnumerator SpawnObjects()
 {
     tempVector = new Vector3(0.5f, 0.5f, 0.5f);

     while( true )
     {
         Instantiate( objectPrefab, tempVector, Quaternion.identity);
         yield return new WaitForSeconds( 1.0f );
     }
 }

}

Comment
Add comment · Show 2 · 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 Mike 3 · Jun 18, 2010 at 09:27 AM 0
Share

There isn't anything wrong with your script that I can see - make sure this script isn't on the prefab you're instantiating perhaps, that'd cause a crazy amount of objects

avatar image dataxpress · Nov 08, 2012 at 06:14 PM 0
Share

Double-plus agree with $$anonymous$$ike 3 here.

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

1 Person is following this question.

avatar image

Related Questions

Linking prefabs to classes that are not attatched to gameobjects 1 Answer

How to properly change variables of a sub class 0 Answers

class design preference 1 Answer

Make objects randomly fall 1 Answer

How to generate a random trajectory when the ball is kicked ? 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