Another instantiate question
Hello, I am total beginner. I've been trying to loop a Background image (haven't found/comprehended any tiling methods which could achieve my desired result) by instantiating the gameObject that my script is attached to and then moving them constantly. When they get to a certain position, I rearrange one of them to match the width of the other one (so i can create an endless loop).
When I try instantiating, I get an instance every frame. I have also already seen some of the questions and answers regarding this topic but the code is not working as I'd like:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BGLoop : MonoBehaviour {
float bgWidth;
float width;
bool instanced;
void Start () {
SpriteRenderer bg = GetComponent<SpriteRenderer> ();
instanced = false;
if (Camera.main) {
float height = Camera.main.orthographicSize * 2;
width = height * Camera.main.aspect;
bgWidth = bg.bounds.size.x;
float bgHeight = bg.bounds.size.y;
float newY = height/bgHeight;
float newX = width/bgWidth;
if (instanced == false) {
Instantiate (gameObject, new Vector3(width,0,0), Quaternion.identity);
instanced = true;
}
}
}
void FixedUpdate(){
if (transform.position.x <= -width) {
transform.position = new Vector2 (width, 0f);
}
}
void Update () {
this.transform.Translate (-2f * Time.deltaTime, 0, 0);
}
}
Why is it instantiating every frame?
Your answer
Follow this Question
Related Questions
How do I fix my scrolling background,How do I fix my scrolling background? 0 Answers
How do I remove the seams from my scrolling background? 0 Answers
Application Run In Background 1 Answer
Problem with script in multiple scenes needing to refer to FPS Controller. 1 Answer
Xbox 360 3rd Person Character 0 Answers