Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mochiponzu · Aug 09, 2019 at 11:55 PM · spritetexture2dslow

It's not possible to do Sprite.Create on one line and assign it on the next one. What are my alternatives here?

What I'm trying to do is create an afterimage trail of a sprite character. To achieve this, I thought I'd use prefabs that get instantiated with the current frames sprite and then fade out while staying in position.

So I added a script that creates the prefabs and passes some variables via a method. On the prefab itself, I have a Sprite Renderer and another script that contains a death timer and this method:

     public void ChangeSprite(Sprite newSprite, Texture2D texture) // would it be better to pass texture by reference here, so the texture isn't in memory twice?
     {
         //spriteRenderer.sprite = newSprite; // <-- This was my first naive attempt
        displaySprite = Sprite.Create(texture,new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);
         spriteRenderer.sprite = displaySprite;
     }

As you can see, I first thought it would be enough to just pass the sprite itself. The prefabs do get instantiated, but the Sprite Renderers' sprite fields are all empty. I looked around the manual and found the Sprite.Create method, which I think I basically used correctly in the above example. However, above code gets me a NullReferenceException: "Object reference not set to an instance of an object" that points to the line that says "displaySprite = ..." in my script. The main difference between my script and the example code from the manual is that in the manual, the Sprite.Create function is in the start method and the sprite it creates gets used later.

When I was reading about Sprite.Create, I found some threads of people who thought it was slow, so I tried setting up a one second timer inside the prefabs Update function, that would call another method to actually change the sprite to the one created in ChangeSprite (which I guess is a misnomer then) and that actually worked.

Now I could perhaps set the timer for the sprite change as low as .05 or so and see how that turns out, but it's still bothering me. Even though I'm not planning on using this prefab excessively, only on some actions the main character performs, and then probably not on every frame either. It might not be a huge impact, but maybe in the future I'll want to dynamically assign more sprites and the load might add up? Isn't there a better way to do this? I'm just trying to pass the exact sprite/graphic the main character is displaying at that point to a prefab... Also, can someone answer the question in the code, about passing the texture2D by reference? Is that better/faster in this case, or is the difference negligible? As I understand it, passing it by value will create another texture2D in memory.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mochiponzu · Aug 10, 2019 at 12:33 AM

I actually found an answer by chance, so I'll put it here in case someone might be interested. The above problem and more specifically the NullReference wasn't caused by the Sprite.Create and the sprite not yet being ready. Everything was fixed when I changed the GetComponent from the Start method to awake. Apparently, if you instantiate a prefab and then immediately call a method within that prefab (is there any other way to pass variables to a prefab?), then that method might be executed before the start method, so the components aren't yet accessible. Still, if someone could elaborate on some of the other questions I asked or suggest a better way to do this altogether, that would be great.

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

145 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Loading a Sprite (Unity 4.3) in resource folder and setting it in sprite renderer 6 Answers

Combine Array of Sprites to Form One Sprite 0 Answers

Reference Specific Sprites from Atlas 1 Answer

iOS crash with "Memory pressure" error - too many sprites? 0 Answers

Unity change my color variation?? 0 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