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 Mark Grob · Aug 30, 2010 at 06:25 PM · arrayspawninvokerepeating

InvokeRepeating stops repeating???? 4-5 instances of an object in array breaks it. why?

I am having a hard time getting my invokeRepeating () to working after it spawns 4-5 objects from an array. Comments?


var SpawnMax = 0; var SpawnCount = 1; var sphere : GameObject; var waitTime : float = 1.0; var lastp : int; var counts : int = 0;

var puzzlepieces : GameObject[];

function Start(){ PlayerPrefs.SetInt("SpawnMax", SpawnMax); PlayerPrefs.SetInt("SpawnCount", 0);
InvokeRepeating("Spawn", waitTime, waitTime); }

function Spawn() {

// SpawnMax = PlayerPrefs.GetInt("SpawnMax"); SpawnCount = PlayerPrefs.GetInt("SpawnCount");

 if (SpawnCount >= SpawnMax){
 }
 else {
         var arraymax = puzzlepieces.length;
         if (PlayerPrefs.GetInt("match") == 1){
             var killp = PlayerPrefs.GetInt("currentp");
             var arr = new Array(puzzlepieces);
             arr.RemoveAt(killp);
             puzzlepieces = arr;
             PlayerPrefs.SetInt("match", 0);
         }       
         myrandominstance = (Random.Range(0,arraymax));
         if (myrandominstance == lastp){
             myrandominstance = (Random.Range(0,arraymax));
         }
         else if (myrandominstance != lastp){
             PlayerPrefs.SetInt("currentp", myrandominstance);
             var i = PlayerPrefs.GetInt("currentp");
             sphere = puzzlepieces[i];
             sphere = Instantiate(sphere, transform.position, transform.rotation);
             PlayerPrefs.SetInt("SpawnCount", SpawnCount);
             SpawnCount = SpawnCount+1;
             lastp = i;
         }           
 }

}

Comment
Add comment · Show 1
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 Eric5h5 · Aug 30, 2010 at 09:40 PM 0
Share

You shouldn't be abusing PlayerPrefs like that...that causes unnecessary I/O. Only use them when necessary, and otherwise just use variables.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bampf · Aug 30, 2010 at 08:32 PM

It's unlikely that the problem is with InvokeRepeating, but with the way your function Spawn works. Put some Debug.Log lines in there (or use Unity 3's source-level debugger if you have it) and see what's really going on. I can see a couple of ways the function could be getting called and then do nothing.

1) Check to see what MaxSpawn is set to. Once it's been reached, the function returns without doing anything. So make sure its set to a number as high as you expect.

2) Possibly bug: The following code may not be doing what you expect:

    myrandominstance = (Random.Range(0,arraymax));
    if (myrandominstance == lastp){
        myrandominstance = (Random.Range(0,arraymax));
    }
    else if (myrandominstance != lastp){

It looks like it's trying to pick a random puzzle piece. However, if the piece is the same as last time (lastp) it's trying to pick another random number. The first oddity is that the new number may also be the same as lastp-- the code doesn't check. Secondly, the ELSE line prevents any piece from getting instantiated and the function will exit.

3) Items are getting deleted from the puzzlepieces array if some kind of match was made (I'm guessing here because that code isn't shown.) The array of pieces seems to get smaller and smaller and eventually nothing will spawn. I'm not sure if that's by design.

Probable bug: note that removing an item from the list means that the index you saved for the last piece spawned (lastp) will sometimes point to the wrong puzzle piece. Removing an item from the middle of the list changes the indexes of all the puzzle pieces that were after it in the array.

Also note also that when the list gets small, it becomes more and more likely that lastp will equal the random number. It is even possible that the final piece will never spawn, if lastp equals 0.

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 ShowyAJ · Oct 26, 2010 at 05:55 AM

Do you have any timers in this or in other scripts that might be Destroying spawned items automatically or after a certain period of time? That timer could be messing things up.

I just had a similar issue with invokerepeating but when i turned off the destroy timers I had, the problem went away.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Creating a looping spawn with an Array 0 Answers

How to fix some of location have more than 1 object? 2 Answers

random spawn locations.... 2 Answers

Spawn "n" Enemies Every "nth" Wave? 1 Answer

Checking if a position is occupied in 2D? 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