Object still instantiated after the function stops being called

Hey there, I asked this in Unity answers but couldn’t really understand the explanations I was given. So basically I was following a tutorial and its about tiling backgrounds. So when I call the function from the if statment, the boolean hasARightBuddy (and hasALeftBuddy) set to true, so that the function won’t be called again.

 if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX  hasARightBuddy == false)
{
MakeNewBuddy (1);
hasARightBuddy = true;
}
else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX  hasALeftBuddy == false)
{
MakeNewBuddy (-1);
hasALeftBuddy = true;
}
}
}

However this just isn’t the case. If i leave this code out then the object is STILL instantiated. So naturally I’m wondering if the function is no longer being called, then why is the object still instantiating. Why the need for the code at the bottom?

if (rightOrLeft > 0) 
		{
			newBuddy.GetComponent<Tiling>().hasALeftBuddy = true; // the newBuddy of our tiling script that is instantiated on the left (the instantiated object that met the conditions of hasALeftBuddy...that was invoked invoked)
		}
		else
		{
			newBuddy.GetComponent<Tiling>().hasARightBuddy = true; // the newBuddy of our tililng script that is instantiated on the right
		}

So after trying to use a prefab (instead of the actual gameObject that already exists in scene), it turns out that it doesn’t work, and it doesn’t work because the prefab that is created is actually asking for another prefab to be dragged in so that it can keep duplicating them. so yea kind of interesting. Not 100 percent sure on how the code works, but so far that’s what I have found out.

awesome, half the work helping you is already done… how about a link?

Here’s the link to the tutorial if you wanted to see it

https://www.youtube.com/watch?v=UbPiCgCkHTE

if (rightOrLeft > 0)
{
newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
}
else
{
newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
}

I actually have a new problem, and thought I would post it here rather than try and start a new thread. Basically I can’t really make a connection with the booleans in the last if and else statements in the script. In order to try and make sense of it, I wrote out a sentence.

if the parameter is -1 have the variable (newBuddy) of this script given the property hasALeftBuddy = true because the hasALeft Buddy Boolean…

and then I draw a blank. Does anybody think they can help me with the rest of the logic? Does it cut off the entire Update function?

What is between offsetX and hasARightBuddy? is it “||” or “&&”?

It’s &&. Sry I left that out. Here is the whole script if you want to look it over as well.

    using UnityEngine;
    using System.Collections;
    
    [RequireComponent (typeof(SpriteRenderer))]
    
    public class Tiling : MonoBehaviour {
    
    public int offsetX = 2;
    
    public bool hasARightBuddy = false;
    public bool hasALeftBuddy = false;
    
    public bool reverseScale = false;
    
    
    private float spriteWidth = 0f;
    
    private Camera cam;
    private Transform myTransform;
    
    void Awake () {
    cam = Camera.main;
    myTransform = transform;
    }
    
    // Use this for initialization
    void Start () {
    SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
    spriteWidth = sRenderer.sprite.bounds.size.x;
    }
    
    // Update is called once per frame
    void Update ()
    {
    
    
    if (hasALeftBuddy == false || hasARightBuddy == false)
    {
    
    float camHorizontalExtend = cam.orthographicSize * Screen.width/Screen.height;
    
    
    float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend; //sprite width/2..51.2
    //(0 + 51.2 - 26.67315 = 24.52685)
    //(102.4 + 51.2 - 26.67315 = 126.92685) etc.. //clone of the myTransform
    float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizontalExtend; // (0 - 51.2 + 26.67315 = -24.52685)
    // (-102.4 - 51.2 + 26.67315 = -126.92685) etc..//clone of the myTransform
    
    if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
    {
    MakeNewBuddy (1);
    hasARightBuddy = true;
    }
    else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
    {
    MakeNewBuddy (-1);
    hasALeftBuddy = true;
    }
    }
    }
    
    
    void MakeNewBuddy (int rightOrLeft)
    {
    
    Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
    
    Transform newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform;
    
    
    if (reverseScale == true)
    {
    newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
    }
    
    
    if (rightOrLeft > 0)
    {
    newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
    }
    else
    {
    newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
    }
    }
    }
    }