2
\$\begingroup\$

Hello everyone and thank you so much for your help! I've tried following the answer at Unity 5 2D drawing sprites programmatically but nothing happened. I have a blank screen with just a background image (my game is 2d). I want to programmatically create 6 (or more) platforms (rectangles) one above another in the center of the camera view. If you've played the game Doodle Jump - that's sort of what I want to accomplish, but the movement is for a later day. My background is in swift and SpriteKit developing for iOS. I'm completely stumped here. I've tried to create a gameObject and then add the component SpriteRenderer and then tried to create a square texture which I can scale into a rectangle. But no platforms appear. I'm really at a loss of what to do because I don't see how to create a sprite. I've tried the Sprite.Create method but that didn't work for me either. Please will you help me?

\$\endgroup\$
2
  • \$\begingroup\$ Please show us the code you've written, how you've set up your scene, and what you see in the scene view, hierarchy, and console tabs when the script is running. \$\endgroup\$
    – DMGregory
    Commented May 9, 2023 at 23:41
  • \$\begingroup\$ Thank you for your comment. I've found a solution so I'll write that below \$\endgroup\$
    – TOM
    Commented May 11, 2023 at 16:34

1 Answer 1

2
\$\begingroup\$

I found a solution while looking through a tutorial. I created a prefab of the platform I wanted to programmatically create. Then I wrote the following inside a for loop:

GameObject platform = Instantiate(platformPrefab, spawnPosition, Quaternion.identity);

This created the 6 platforms I needed through iteration. spawnPositon.y increased at every loop. Quaternion.Identity is so the platforms aren't rotated.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .