Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

SpriteManager - draw lots of sprites in a single draw call!

Discussion in 'iOS and tvOS' started by Brady, Jan 15, 2009.

  1. bobber205

    bobber205

    Joined:
    May 12, 2009
    Posts:
    139
    Aah. I'm windows only atm.
    Just curious. I assume I can use this library on windows/OS X platforms with no issues right? :)
     
  2. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Ahh yes, compression is another likely culprit. In general, for problems like this, I'd advise always reviewing mipmapping, compression, and UVs (sometimes using the "correct" UVs leads to rounding error which goes just beyond the "correct" values, so you have to pull them in a smidge).

    Your method of saving all the positions to a datafile allowing WYSIWYG layout is a great approach!
     
  3. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Yes, SpriteManager should work in "regular" Unity as well and can be useful in reducing draw calls. Though the package I attached above is a project for iPhone, so it (the package) probably won't work for anything but Unity iPhone.
     
  4. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    RemoveSprite(Sprite) followed immediately by Destroy(this.gameObject) seems to throw up some timing issues in that the object can be destroyed before the sprite is removed.

    Any ideas on the best way to tackle this?

    thanks
     
  5. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    RemoveSprite() is a blocking function, meaning it won't return until it is finished. So a Destroy() call made after RemoveSprite() in the same procedure should be safe since Destroy() won't be called until after RemoveSprite() has returned.

    However, I did discover a potential problem: the "client" member of the sprite being removed was being set just prior to being removed from the active list of sprites. That could be a problem since Update() and LateUpdate() are called asynchronously and could conceivably be in the process of being called in the middle of a RemoveSprite() call. So now RemoveSprite() looks like this in my test version:

    Code (csharp):
    1.     public void RemoveSprite(Sprite sprite)
    2.     {
    3.         sprite.SetSizeXY(0,0);
    4.         sprite.v1 = Vector3.zero;
    5.         sprite.v2 = Vector3.zero;
    6.         sprite.v3 = Vector3.zero;
    7.         sprite.v4 = Vector3.zero;
    8.  
    9.         vertices[sprite.mv1] = sprite.v1;
    10.         vertices[sprite.mv2] = sprite.v2;
    11.         vertices[sprite.mv3] = sprite.v3;
    12.         vertices[sprite.mv4] = sprite.v4;
    13.  
    14.         availableBlocks.Add(sprite);
    15.  
    16.         // Remove the sprite from the billboarded list
    17.         // since that list should only contain active
    18.         // sprites:
    19.         if (sprite.billboarded)
    20.             activeBillboards.Remove(sprite);
    21.         else
    22.             activeBlocks.Remove(sprite);
    23.  
    24.         sprite.client = null;
    25.  
    26.         sprite.billboarded = false;
    27.  
    28.         vertsChanged = true;
    29.     }
    So try that. Just move "sprite.client = null;" to after the part where it removes the sprite from either the activeBlock list or activeBillboards.

    Let me know if that fixes your problem. That may also have been part of dock's problem. If you're still there, dock, please let me know if this helps.
     
  6. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Thanks for the quick reply - will give it a whirl and let you know
     
  7. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Something else I neglected to do: move "availableBlocks.Add(sprite);" to AFTER "sprite.billboarded = false;"

    That'll make sure asynchronous routines won't catch a sprite on the available list until after it's already been thoroughly removed from the active list and "cleaned".
     
  8. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    If I use HideSprite(Sprite) and the sprite is not already hidden then HideSprite is called twice. First call is from the calling script HideSprite(sprite) and then in SM it sets Sprite.hidden which will call HideSprite() again.

    I am still looking at why when I use HideSprite() the sprite is still displayed and a Debug shows HideSprite() being called twice but no ShowSprite.

    any ideas?

    thanks
     
  9. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    This sounds like another synchronization problem. I've moved the vertex assignment to after the sprite is added to/removed from any list since an asynchronous LateUpdate() could potentially re-assign the vertices via a transformation in between the assignment in HideSprite() and its removal from the active list, thereby undoing the "zeroing" of the vertices. Try the following versions of HideSprite/ShowSprite and let me know if that fixes the problem:

    Code (csharp):
    1.     public void HideSprite(Sprite sprite)
    2.     {
    3.         if (sprite.hidden)
    4.             return;
    5.  
    6.         // Remove the sprite from the billboarded list
    7.         // since that list should only contain sprites
    8.         // we intend to transform:
    9.         if (sprite.billboarded)
    10.             activeBillboards.Remove(sprite);
    11.         else
    12.             activeBlocks.Remove(sprite);
    13.  
    14.         sprite.hidden = true;
    15.  
    16.         vertices[sprite.mv1] = Vector3.zero;
    17.         vertices[sprite.mv2] = Vector3.zero;
    18.         vertices[sprite.mv3] = Vector3.zero;
    19.         vertices[sprite.mv4] = Vector3.zero;
    20.  
    21.         vertsChanged = true;
    22.     }
    23.  
    24.     public void ShowSprite(Sprite sprite)
    25.     {
    26.         // Only show the sprite if it has a client:
    27.         if(sprite.client == null)
    28.             return;
    29.  
    30.         if (!sprite.hidden)
    31.             return;
    32.  
    33.         sprite.hidden = false;
    34.  
    35.         // Update the vertices:
    36.         sprite.Transform();
    37.  
    38.         if (sprite.billboarded)
    39.             activeBillboards.Add(sprite);
    40.         else
    41.             activeBlocks.Add(sprite);
    42.  
    43.         vertsChanged = true;
    44.     }
    Once I have a chance to make sure all of this is solving these problems, I'll integrate these changes into the version on the Wiki.
     
  10. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Thanks Brady.

    after looking at this further the issue is that a sprite can be listed in SM as hidden == true when its false. I have several sprites (each with different named references) and as the final sprite enters the gameboard the previous ones have been Removed and their GOs destroyed. The final sprite shows as hidden == true when its false as it seems to pick up from the previous sprite.

    The workaround I am using at the moment is to reset the sprite hidden value:


    Code (csharp):
    1. public void ResetSpriteShow(Sprite sprite)
    2.     {
    3.         sprite.hidden = false
    4.     }
    Initial tests show this to work but I need to work out a better solution than this hack.

    thanks for the help with this (much appreciated)
     
  11. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    After more investigation the problem seems to be with RemoveSprite(). If you remove a sprite and then reuse the same slot the hidden bool is not reset so the new sprite will be shown as hidden when its showing.

    Placing sprite.hidden = false; in the RemoveSprite method fixes it.

    thanks Brady
     
  12. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Okay, thanks for finding this. In my latest internal version, I'd created a section in RemoveSprite() that makes sure all these "states" and flags get reset for the sprite being removed, but somehow I missed that one. I've also made a change to how the "m_hidden" flag is accessed. Since C# doesn't have "friends", I had to rename it to the ridiculous name of "m_hidden___DoNotAccessExternally" so that nobody would ever accidentally use it, but I had to make it public so SpriteManager could access this flag directly to keep from having to recursively call the Sprite's own "hidden" property. This should mean if you're doing a large batch of hides/unhides, it should run a lot faster. Anyway, I should have all of this packaged together soon and I'll post it.

    Thanks again! (BTW, did any of those other changes fix the crashes, etc?)
     
  13. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Not sure yet. The game Adds/Removes Hides/Shows sprites as well as parenting/unparenting (and deleting the object) a lot (there are 6 SM's half of which are linked), so a lot more testing needs to be done especially as there could be somewhere between 250 and 300 sprites active in the game. Without SM this game could not have happened.

    thanks Brady
     
  14. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Okay, just let me know if you encounter any other problems like that so I can roll as many fixes as possible into the next release of SM.

    250-300 sprites sounds great! What kind of framerate are you getting with that?
     
  15. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    At the moment worst case is around 30. Camera zoom hurts it more than anything. There are no physics in the game and the draw calls will probably end up around 14 or 15 in total. I use LinkedSM for the GUI (its animated) which is a huge help.

    Swapping out sprites (coroutine) works well and gives some very nice explosion effects. It was one of these type of effects that was still showing when should have been hidden.
     
  16. RedJester

    RedJester

    Joined:
    May 21, 2009
    Posts:
    2
    So using your demo project, I've been trying to get something that's in billboard form to rotate along an axis. In this case the X axis. The sprite manager is set up for XY sprites.

    I've been trying to use this code on the game object that the sprite is attached to

    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);
    When the sprite is not billboarded, it seems to rotate on all three axies, even when I am really only accepting left/right and up/down inputs. So there might be something odd with my direction.

    Anyway, when it is billboarded, the sprite won't rotate at all.

    So, here's what I want to do. I have a sprite that starts "up" at 270 degrees. When I push left or right I want the sprite to move left or right and also rotates to point towards its new rotation.

    Is this the way to go, or should I really just do the normal way of using sprites, where I have my character pointing in all directions on my model sheet, and I switch to a different UV section when he turns?
     
  17. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Billboards don't rotate. If you want rotating sprites, you'll need to make sure they aren't billboarded. I'm not sure exactly how you're wanting the sprite to rotate, but it may be that you don't really want to use LookRotation to accomplish the type of rotation you're wanting. I'm not sure, but I'd say look into how you're rotating your GameObject, because as long as it isn't billboarded, a sprite will follow the GameObject it's associated with perfectly. So if it isn't rotating right, look into how you're rotating the GameObject - it shouldn't be a sprite issue.
     
  18. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Brady,

    seems (so far) that the changes have stopped the RemoveSprite() crashes :)

    Looks like I will have to modify SM slightly now as I need to be able to create hidden sprites rather than my current method of creation followed by an immediate HideSprite();

    Thanks
     
  19. mctricks

    mctricks

    Joined:
    May 25, 2009
    Posts:
    151
    Hey could somebody help me with this? I started using this about 5 days ago, and it worked perfectly...

    But all of a sudden, my sprites are really fuzzy...
    Help?
     
  20. mctricks

    mctricks

    Joined:
    May 25, 2009
    Posts:
    151
    Well, that's weird... it just fixed itself...

    nvm
     
  21. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    It's probably PVRTC compression. Try changing the import settings of the texture.
     
  22. Rapzid

    Rapzid

    Joined:
    Apr 25, 2009
    Posts:
    123
    Just wanted to share an experience. I have a camera following a physics object and I'm using SpriteManager to overlay some information icons. Due to the issues inherent with chasing a physics object something always seemed to be 'bouncing' in the camera. After I finally thought I had it all worked out execution order changed on me yesterday and I had to find another solution. I thought there must be a way to make sure something happens after all physics are calculated for the frame. So I dug up OnPreRender(). This gets called on the camera right before it renders the scene. So I changed LinkedSpriteManager's TransformSprites() to public and dumped all of LateUpdate into it. Now I manually call TransformSprites from the camera's OnPreRender(). So far so good.
     
  23. MonkeyTrax

    MonkeyTrax

    Joined:
    Mar 31, 2009
    Posts:
    34
    Brady,

    Thanks for contributing all your hard work on SpriteManager to the community, we appreciate it.

    Just installed the package and plan to dig into the code starting Monday. I wanted to ask if you have an ETA for the next update? I'd rather hold off on the digging if we're talking days rather than weeks.

    Thanks
     
  24. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    I'm going to try and get it together in the next week. I can't promise anything, but that's the plan. Thanks for the kind comments!
     
  25. hellomrjoyboy

    hellomrjoyboy

    Joined:
    Feb 2, 2009
    Posts:
    6
    I would like to use two different materials for the Sprite manager. (one is a Particles/Alpha Blend and the other is a Particles/Additive (Soft) ). How would you recommend approaching this? I have tried several ways, none of which have succeeded.

    The situation is a gameObject with a sprite on collision gets its sprite swapped out with a new one, but this new one is from a different material.

    Thanks.
     
  26. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    The entire objective of SpriteManager is to be able to combine multiple, independently-moving objects into a single draw call to avoid the considerable overhead of multiple drawcalls. To combine two objects into a single draw call, they must share the same material. So using a different material would defeat the purpose. However, if you have so many textures that they can't all fit onto one, or if you want to use a different shader, as in your case, then the next best thing would be to use a separate SpriteManager for each material. But you'll want to consolidate as many objects as possible into each SpriteManager to avoid extra draw calls.
     
  27. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Okay, folks! Version 0.63 is now available:
    http://unifycommunity.com/wiki/index.php?title=SpriteManager

    This release includes several recently posted bug fixes, as well as an all-new animation system. The animation system makes it easy to define multiple animations. You can even manually define arbitrary UV coordinates for composing your animation, if you need. It supports looping, "rocking" (playing forwards, then backwards), reverse playing, and pausing/unpausing.

    I've added documentation to explain the new animation routines and class to the wiki, but I'll give a brief explanation of how to tie it all together here.

    First, you will create a new UVAnimation object:

    Code (csharp):
    1. UVAnimation anim = new UVAnimation();
    Then, you want to tell it about your grid of sprites that make up your sprite animation (see the wiki for a description of the parameters to BuildUVAnim()):

    Code (csharp):
    1.  
    2. anim.BuildUVAnim(new Vector2(0, 0.9f), new Vector2(0.1f, 0.1f), 10, 1, 10, 30f);
    3.  
    Then, you'll want to set a handful of other settings for the animation:

    Code (csharp):
    1.  
    2. anim.name = "walk";  // name of animation
    3. anim.loopCycles = 2; // number of times to loop before stopping
    4. anim.loopReverse = true; // Reverse play back to beginning once the end is reached
    5.  
    Now that the animation is fully defined, we need to add the animation to our sprite:

    Code (csharp):
    1.  
    2. sprite.AddAnimation(anim);
    3.  
    Now we want to "play" the animation:

    Code (csharp):
    1.  
    2. sprite.PlayAnim("walk");
    3.  
    We could also have passed a reference to the animation object itself rather than the string "walk", which would be a bit faster.

    Have fun!
     
  28. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Thanks Brady,

    a herculean effort on your part.

    We are nearing the end of our game dev cycle and have around 6 to 8 SM's (linked and static) threaded all thru the game (our original estimate of up to 350 sprites is probably on the low side). I was thinking of dropping in your latest version. Is that possible to just swap out what is there (bearing in mind that at present we don't need the new features) ?

    thanks again Brady this is an incredible resource for everyone.
     
  29. joelmgallant

    joelmgallant

    Joined:
    Mar 9, 2009
    Posts:
    113
    Indeed, thanks a ton for this! Animation support is just what I wanted :)

    The only thing that didn't work right out-of-the-box for me was the SetHidden command - I see it's been changed to HideSprite and ShowSprite - more logical.

    Keep up the great work!
     
  30. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    imparare, I believe you should be able to just drop it in. I don't believe the new features or bug fixes should change expected behavior. If you find out differently, however, you should just be able to switch script files back to the previous version you have. Be sure to drop me a note when your game is released, I'd love to see that many sprites in action!

    0turner0, thanks, the sprite hiding should work better now, that was part of where I fixed up some code.
     
  31. MonkeyTrax

    MonkeyTrax

    Joined:
    Mar 31, 2009
    Posts:
    34
    Brady: Thanks again for all the hard work.

    Is there a demo project for the v0.63 release?
     
  32. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    I'm afraid not. I haven't done up a version of the demo project that uses animation. But the sample code in my earlier post regarding animation, combined with the docs on the wiki should get you up and running. When I have more time I'll try to integrate this into the demo project.
     
  33. Hello World

    Hello World

    Joined:
    Jun 4, 2009
    Posts:
    2
    Just wanted to say awesome job, I was thinking of a way to implement a 2D sprite system into Unity when I stumbled across this post. To say the least you saved me hours of work, and quite possibly a few headaches.

    I would consider myself a little more than an intermediate programming, but not quite professional (I'm working on it). That being said I have a solid understanding of how the manager operates. However, I've seen it here and other places and it still puzzles me. When you're defining any sort of coordinate you append "f" to the value(s). I would assume this has something to do with world and pixel space? If you could enlighten me that would be greatly appreciated.

    John
     
  34. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    C# is a strictly-typed language, so the 'f' tells C# that the value is single-precision floating point value. Leaving off the 'f', it would be assumed to be of type "double" (double-precision floating point).
     
  35. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Hi Brady,

    looking thru SM (old version) I don't see anything obvious to update the Sprite Width and Height. I guess transform is an option somehow or even remove and re-add the sprite with the new values would be a reasonable method.

    Am I missing something more obvious?

    thanks as always
     
  36. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Yes, check the Sprite class. There are methods there to set the width and height.
     
  37. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Yep something obvious then. Was only looking at the SM and Linked classes.

    thanks Brady
     
  38. Hello World

    Hello World

    Joined:
    Jun 4, 2009
    Posts:
    2
    Yeah I'm a goofball... Thanks, that makes perfect sense.

    I've never read a primer on C#, still new to the language. I do have text on it, perhaps I should read it.

    Do you prefer using C# in Unity versus JavaScript? If so why.
     
  39. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    I do prefer C#, partially because its syntax is more explicit than JavaScript (which is one reason some people prefer JavaScript), and partially because my "native" language is C++. :)
     
  40. fehaar

    fehaar

    Joined:
    Mar 1, 2007
    Posts:
    44
    I also prefer C# due to it being strongly typed. It may seem like a pain in the rear to have to cast objects to get their correct type - but in the end it can save you from some extremely hard to find bugs due to lack of type safety in JavaScript.
     
  41. BasketQase

    BasketQase

    Joined:
    Sep 12, 2005
    Posts:
    97
    Sorry for the very basic question, but how do I download this tool? Do I copy and paste the code from the wiki to a blank document? Is there a downloadable archive of the scripts? Also, I read mention of an example project, where would one find that?

    Thanks, and I apologize again for the very basic question,
    ~Rob

    [edit: My bad . . . didn't realize two things: a) that I needed to be logged in to see the attachment links, and b) that I wasn't logged in (until I went to make this reply) . . . d'oh! :D ]
     
  42. hellomrjoyboy

    hellomrjoyboy

    Joined:
    Feb 2, 2009
    Posts:
    6
    I am using Unity iPhone 1.0.2f4 (22303)

    I attached to this post two unity packages one using the new Sprite Manager and one using the previous SM version. I also attached a video of the Unity workspace to illustrate what I am seeing.

    Question 1:
    I am seeing a duplication of GameObjects which have a sprite attached to them. In the video, the duplicates are at the top (Cube1 and Cube2). Is this expected behavior with the new SM? It doesn't appear with the older version.

    Question 2:
    In the video I perform a little checking and unchecking of GameObjects which hold the LSM script and the SCD script. Notice that when this is completed, and the project is run, three GameObjects appear named Temp. Each of which have the Sprite script attached to them. Is this expected behavior?

    Question 3:
    Perhaps I am setting up the project incorrectly, therein causing this behavior? The set up I am using works for the older version of the SM.


    Thanks.
     

    Attached Files:

  43. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    Actually, I had neglected to update the attachments to the first post. I've now fixed that, sort of, by updating the SpriteManager.cs and LinkedSpriteManager.cs files. However, the demo project has not been updated with the latest version, and for some strange reason, it won't let me post the new Sprite.cs file, which is necessary to make the whole thing work with the latest version. I'll probably just wind up removing the .cs files and see if it'll let me upload a single .zip.
     
  44. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    hellomrjoyboy,

    Yes, this is expected behavior. The reason is, to enable the new animation feature, the Sprite class has to derive from MonoBehavior so that it has access to routines like Invoke and InvokeRepeating. All objects of type MonoBehavior must have an associated GameObject. At first you would think of just attaching the object to the client GameObject which the Sprite represents. However, this will not work since that GameObject can come and go, while the Sprite object must persist. So the solution was to instantiate a dummy GameObject for each Sprite object that gets created. Just disregard the extra GOs, unfortunately, they're necessary because of the way MonoBehavior works.
     
  45. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,082
    Great job on the animation support. Thanks a lot! I have one suggestion though:

    Include a function similar to BuildUVAnim, in that you can specify a grid of cells, but also pass an array that indicates the indices of the cells to use as frames. So you could have the frames in a different order, or have a certain frame play a few times in a row to have a pause in the animation.

    I know I can custom-code this, but it would be nice to have this built-in.
     
  46. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    That's a good idea, thanks for the suggestion!
     
  47. anthropophagy

    anthropophagy

    Joined:
    Apr 28, 2009
    Posts:
    10
    Hey everyone,

    Brady, thank you so much for this


    I have a little bug that I'm having a hard time figuring out. I have a pretty basic little set up where the user touches the screen and it instantiates a sprite (a prefab). The sprites move around with physics and collisions and then fall off screen into a trigger that removes the sprite and destroys the game object.

    This will work fine for a period of time, but then eventually I will get the error "Object reference not set to an instance of an object" -- the line draws itself to my destroy trigger, and double clicking on the error takes me to the Sprite.Transform() function...


    any ideas off the tops of your beautiful minds?


    as I said everything works nicely for the most part, but it just happens after a bit. It seems to be tied to the allocBlockSize, but I monitored the availableBlocks and the activeBlocks and it never had to allocate more sprites or anything like that, it would just balance nicely then suddenly, and in no real predictable pattern, there would be one too many in the availableBlocks and then it throws me the error and hickups/crashes.


    any help would be greatly appreciated. Thanks again to Brady and the rest of the community, yous are princes.
     
  48. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,301
    That will happen when the client object of a sprite has been deleted before the sprite itself has been removed. What's happening is the object is being destroyed, so the client reference in the Sprite object is no longer valid. On the next go-around, it tries to transform the sprite by that object's transform, but when it attempts to dereference it to access its transform, it will raise an exception.

    Make sure you're always removing the sprite from the SpriteManager BEFORE you delete the associated game object.
     
  49. dock

    dock

    Joined:
    Jan 2, 2008
    Posts:
    376
    Thanks for the update to the SpriteManager Brady! I'm still using the older version for this project, but I'm look forward to using the new features for my future projects! ^-^
     
  50. anthropophagy

    anthropophagy

    Joined:
    Apr 28, 2009
    Posts:
    10
    Hey, thanks for the prompt reply,

    I've tried so many things to chase down the problem that I didn't want to include it all... that occurred to me also, so I made sure the Destroy was after the Remove, it was. so I put a little WaitForSeconds call before I destroyed the object: worse. So then I took out the Destroy call completely so the trigger was just removing the sprite and not destroying the GO at all... worse still, it throws me the error first thing instead of working for a while, then breaking.

    this is the script attached to my sprite client prefab (JS, sorry I know a lot of yous are using c#)(and for simplicity, this is the version that just removes the sprite and doesn't worry about the client GO)

    Code (csharp):
    1. name = "BasicBlock";
    2. var SM : LinkedSpriteManager;
    3. var SMGO : GameObject;
    4.  
    5. private var targetSprite : Sprite;
    6.  
    7. function Awake () {
    8.     SMGO = GameObject.Find("SpriteManagerGameObject");
    9.     SM = SMGO.GetComponent("LinkedSpriteManager");
    10. }
    11.  
    12. function Start()
    13. {
    14.     targetSprite = SM.AddSprite(targetObject, 1.1, 1.1, 578, 66, 63, 63, false);
    15. }
    16.  
    17. function kill()
    18. {
    19.         SM.RemoveSprite(targetSprite);
    20.        
    21. }

    then on the dead zone I've got:

    Code (csharp):
    1. function OnTriggerEnter(other : Collider){
    2.    
    3.     if(other.gameObject.name == "BasicBlock")
    4.     {
    5.         var script1 : BasicBlocks = other.gameObject.GetComponent("BasicBlocks");
    6.         script1.kill();
    7.     }
    8. }

    obviously it's something I'm doing if nobody else has run into this... any other thoughts? JS users, what am I doin wrong??

    thanks again