1
\$\begingroup\$

I'm new in Unity, so I hope you guys can help. I'm developing a 2D-game for Android device, on the scene I have a huge background image inside Scroll View (UI - Scroll View).

Next, I created new empty Game Object "Players", attached to it "Players script" and added "Sprite Renderer" component.

From the Sprites I created two prefabs - housePrefab and dogPrefab. Inside "Players script" I have the following code:

[SerializeField] GameObject housePrefab;
[SerializeField] GameObject dogPrefab;

void Start()
{
var houseXPos = 1.41f;
var houseYPos = 2.63f;

var dogXPos = -0.41f;
var dogYPos = 2.63f;

GameObject house = Instantiate(housePrefab, transform.position = new Vector2(houseXPos, houseYPos), Quaternion.identity) as GameObject;
GameObject dog = Instantiate(dogPrefab, transform.position = new Vector2(dogXPos, dogYPos), Quaternion.identity) as GameObject;
}

After I run game both Sprites appears on the screen (as I expected). The problem occurs when I Scroll background image, both sprites stay in the same position under scrolling. In other words sprites not scrolling with the background image.

As far as I understand, the Sprites needs to be added to the background image, but I don’t know how to do it. How to make Sprites scroll with the background image?

Thanks to all in advance.

\$\endgroup\$
0
\$\begingroup\$

you back-Ground is a UI in canvas but you playing objects are in the scene and not dependent. your approach is wrong.

if you need a background for you game, just make a simple 2d object with spriteRenderer and scale it to fill the camera view. then make it child of the camera(drag in hierarchi and drop on camera)

Then you can define any behaviour for your characters.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ virtouso, thank you for your answer. Here is what I did: 1. Created a new Scene 2. Hierarchy - 2D Object - Sprite 3. Inspector - Sprite Renderer - Sprite, selected my background image 4. In hierarchy I drop object that I created in step 2 on the camera 5. Pressed play. I see background image, but it’s not scrollable I definitely did something wrong, but I’m not sure what. \$\endgroup\$ – dakath Aug 26 '19 at 19:17
  • \$\begingroup\$ @dakath because that a UI. ui is over everything in game. you shouldnt use UI as background \$\endgroup\$ – virtouso Aug 26 '19 at 19:21
  • \$\begingroup\$ Ok, what should I use instead of UI? Could you please, explain in more detail? Or link me to some tuts? I googled how to make the scrollable background and most of the tuts say use UI - Scroll View \$\endgroup\$ – dakath Aug 26 '19 at 19:29
  • \$\begingroup\$ @dakath i just said in answer. a 2d object and scale to fill the background. and z value less than all objects in the scene \$\endgroup\$ – virtouso Aug 26 '19 at 19:31
  • \$\begingroup\$ I still not get how to do that, but it's ok, will try to find an answer by my self. Thank you again for your time and help. \$\endgroup\$ – dakath Aug 26 '19 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.