- Home /
Question by
koumeravinus · Feb 14, 2018 at 02:06 AM ·
2d gamebackground
Limit looping background in 2d?
I am making 2D game. I am beginner to unity. The idea is to make the player escape from a park in limited time. I want to add zombies randomly in the park. If the player able to get out from the park in time, he will win. I want the background looping but only for 25 times. If he reach the last background, he will win. Can someone help me or give me better idea?
Comment
Answer by LiamofElites · Feb 14, 2018 at 03:09 AM
your post is kind of misleading, your use of the term "background". But I can help you with the looping problem
To set this up
- Create your zombie GameObject and a new folder in the assets folder called "Resources" this will be used for Resources.Load and Instantiate.
- Next drag your zombie GameObject into the Resources folder to create what is known as a prefab.
- Click on it so the components of the prefab show up in the inspector, on the Transform (very first) component click on the gear>Reset Position so the zombie spawns right on the spawner rather than however far away.
- Add the following code to a CSharp script:
SpawnerScript.cs
int loopingLeft;
int loopingTimeout;
void Awake(){
//assign in Awake just in case
loopingLeft = 25;
loopingTimeout = 0;
}
void Update(){
if(loopingTimeout == 0){
if(loopingLeft != 0){
//clone the zombie
//To clone the zombie drag the original "GameObject" into a new
folder called "Resources" so you can access and duplicate the
zombie using Resources.Load the gameobject you duplicated is called a prefab. The gameObject.transform is so the zombie spawns in the spawner gameobject.
Instantiate(Resources.Load("zombie") as GameObject, gameObject.transform);
loopingLeft--;
loopingTimeout = 100;
}
}else{
loopingTimeout--;
}
}
I'm more of a 3D person but for this they're the same. I hope this was helpful! Contact me if you have any questions/concerns
Thank you very much. But what I really mean is to do the parallax repeating background but not infinite. Sorry if I confusing you. English is not my first language.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152606im_/https://answers.unity.com/themes/thub/images/avi.jpg)