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 SquigglyFrog · Jan 15, 2014 at 09:14 PM · c#2dspritepositioningxna

How can I load and position my sprites in c#?

I'm still trying to learn unity (latest 4.3.3) and coming from a monogame / xna C# background.

In monogame, I could simply run through a List and set each sprites updated position. Childs play.. In unity, this is proving to be an exercise in frustration and I know it has to be something really simple that Im missing..

I have the following in a c# script:


 public class Flake
 {
     public Sprite sprite;
 
     public int col { get; set; }
     public int row { get; set; }
     public float alpha { get; set; }
     public Vector2 pos { get; set; }
     public float scale { get; set; }
     public Vector2 velocity { get; set; }
 
     GameObject menuObject;
 
     Transform t;
 
 
     public Flake(Sprite spr, int c, int r, float a, Vector2 p, float s, Vector2 v)
     {
         col = c;
         row = r;
         alpha = a;
         pos = p;
         scale = s;
         velocity = v;
     }
 
     public void Update(float elapsed)
     {
         if (sprite == null)
             Debug.Log("null sprite!");
 
         alpha -= 45 * elapsed;
         alpha = Mathf.Clamp(alpha, 0, 255);
 
         scale -= 0.055f * elapsed;
         scale = Mathf.Clamp(scale, 0, 2.0f);
 
         pos += velocity * elapsed;
 
         t.localPosition = new Vector3(pos.x, 0, pos.y);
     }
 
 }



In another part of the main script, I am loading my sprites and creating a new "flake" and setting the sprite texture. This "seems" to be working fine.. the sprites are not null..


 public Sprite[] sprites;
 
 List<Flake> flakeList = new List<Flake>();
 
 sprites = Resources.LoadAll<Sprite>("Flakes");
 
 flakeList.Add(new Flake(this.sprites[Random.Range(0, this.sprites.GetLength(0) - 1)], 0, 0, 1, new Vector2(1, 1), 1, new Vector2(1, 1)));



Next, I want to update my sprites positions by calling their update and then this is where I am getting stuck. I have my sprite object, and I have tried playing with the renderer and transforms, but I can not figure out how to make the sprites actually display on the scene and update their positions. I would think its a simple:

 t.localPosition = new Vector3(pos.x, 0, pos.y);

But Im not getting it to work.. So how does one go about making these sprites appear and update their position on screen?? Do I need to instantiate a new gameobject for each? or ??

Any help would be greatly appreciated!

If you want to view the full script, its here: http://pastebin.com/9fqz2wVh
and the main part does derive from a monobehavior.. I dont want the Flake class to update itself really..

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 zee_ola05 · Feb 23, 2014 at 08:40 PM

What you need is to create a Flake Prefab and Instantiate it to add it to the scene..

  1. Create A Flake GameObject: Drag your Flake Sprite Texture to Scene.

  2. Make it a Prefab: Drag your Flake GameObject to Project.

  3. You can load that prefab via Resources.Load() and Instantiate.

Hope it helps.

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

19 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

Related Questions

I want to make a certain sprite spawn somewhere periodically, how? 1 Answer

Unity 2D all objects mash together! 0 Answers

How to make sprites in SideScroller move in smooth curves 0 Answers

Implementing 2D Sprite Collisions 1 Answer

Sprite "face" second sprite only on Z axis 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