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 zillion · Oct 14, 2012 at 01:57 PM · javascriptarrayrandomspawn

Simple array an spawning question

Having just a small amount of trouble with a code I have created.

This code creates an array of 5 game objects, and then assigns one of these random game objects to another variable, called randomSpawn. Each of these gameObjects has the same script on it, named spawnObjects. I am attempting to pick one of these game objects, and call one of its functions from the script.

When I do this, the same function is called for EVERY game object. Is it because they are all using the same script? Or because there is an error in my code?

 var spawnPoints: GameObject[];
 var i: GameObject;
 private var randomSpawn:GameObject;
 
 function Update ()
 {
     randomSpawn = spawnPoints[Random.Range (0,5)];
     //print(randomSpawn);
 }
 
 function Start()
 {
     Invoke("MakeLevel", 3);
 }
 
 function MakeLevel()
 {
     randomSpawn.gameObject.GetComponent(spawnObjects).Spawn1();
     yield WaitForSeconds(3);
     randomSpawn.gameObject.GetComponent(spawnObjects).Spawn2();
 }
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

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Montraydavis · Oct 14, 2012 at 02:00 PM

It's the Invoke, which calls for all objects of the same type.

var Object : GameObject = gameOBject.GetComponent ( blaa ) ;

Object.SendMessage ( "MakeLevel", 3 ) ; // Use this instead of Invoke ! :) Should work like a charm.

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 zillion · Oct 14, 2012 at 02:03 PM

Well, once I get this working, it will hopefully look something like this

 function MakeLevel()
 {
 randomSpawn.gameObject.GetComponent(spawnObjects).SpawnFish1();
 yield WaitForSeconds(3);
 randomSpawn.gameObject.GetComponent(spawnObjects).Spawn2();
 randomSpawn.gameObject.GetComponent(spawnObjects).Spawn4();
 randomSpawn.gameObject.GetComponent(spawnObjects).Spawn5();
 yield WaitForSeconds(3);
 randomSpawn.gameObject.GetComponent(spawnObjects).Spawn1();
 randomSpawn.gameObject.GetComponent(spawnObjects).Spawn4();
 randomSpawn.gameObject.GetComponent(spawnObjects).Spawn2();
 randomSpawn.gameObject.GetComponent(spawnObjects).Spawn3();
 randomSpawn.gameObject.GetComponent(spawnObjects).Spawn5();
 //So on so on, spawning more and more after a longer period of time.
 }
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 Montraydavis · Oct 14, 2012 at 02:05 PM 0
Share

Yeah, drop the Invoke, and use Send$$anonymous$$essage ( ) ins$$anonymous$$d, and the rest should be self-explanatory .

avatar image
0

Answer by zillion · Oct 14, 2012 at 02:04 PM

I have heard from other students that SendMessage is very costly. I am creating this game for an iPhone. What is your suggestion on this?

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 zillion · Oct 14, 2012 at 02:08 PM

@Montraydavis - Tried your code just then, and doesn't seem to compile correctly. Never used SendMessage before.

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 Montraydavis · Oct 14, 2012 at 02:10 PM

@zillion : Here is an example of using SendMessage - http://docs.unity3d.com/Documentation/ScriptReference/Component.SendMessage.html

And if you don't want your CPU to pay those costs, why not call the function from the component directly ?

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
  • 1
  • 2
  • ›

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

Calling random functions 4 Answers

Random spawn timer 1 Answer

How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers

Spawn object in random areas 2 Answers

Spawn random amount of gameobjects 2 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