Infinity for

Hello
I want to say that stupid write “infinity cycle” but…
I looked this tutotial https://www.youtube.com/watch?v=n2fx9rkMMVU and all works fine, this is code:

using UnityEngine;
using System.Collections;
public class CreateWalls : MonoBehaviour {
    bool creating;
    public GameObject start;
    public GameObject end;
    public GameObject WallPrefab;
    GameObject Wall;
    private Camera camera;
    //13 is the Layer number I set to "IgnoreRaycast" in the Inspector.
    void Start () {
        camera = GetComponent<Camera>();
    }
    void Update () {
        GetInput ();
    }
    void GetInput () {
        if (Input.GetMouseButtonDown (0)) {
            Setstart ();
            start.GetComponent<Renderer>().enabled = true;
            end.GetComponent<Renderer>().enabled = true;
        } else if (Input.GetMouseButtonUp (0)) {
            SetEnd ();
            start.GetComponent<Renderer>().enabled = false;
            end.GetComponent<Renderer>().enabled = false;
        } else {
            if (creating) {
                Adjust ();
            }
        }
    }
    Vector3 gridSnap(Vector3 originalPosition){
        int granularity = 1;
        Vector3 snappedPosition = new Vector3(Mathf.Floor(originalPosition.x / granularity) * granularity, originalPosition.y, Mathf.Floor(originalPosition.z / granularity) * granularity);
        return snappedPosition;
    }
    void Setstart () {
        creating = true;
        start.transform.position = gridSnap(GetWorldPoint ());
        Wall = (GameObject)Instantiate(WallPrefab, start.transform.position, Quaternion.identity);
    }

    void SetEnd () {
        creating = false;
        end.transform.position = gridSnap(GetWorldPoint ());
    }

    void Adjust () {
        end.transform.position = gridSnap(GetWorldPoint ());
        AdjustWall ();
    }

    void AdjustWall () {
        float dist = Vector3.Distance (start.transform.position, end.transform.position);
        start.transform.LookAt (end.transform.position);
        end.transform.LookAt (start.transform.position);
        Wall.transform.position = start.transform.position + dist / 2 * start.transform.forward;
        Wall.transform.rotation = start.transform.rotation;
        Wall.transform.localScale = new Vector3 (Wall.transform.localScale.x, Wall.transform.localScale.y, dist);
    }
    Vector3 GetWorldPoint () {
        Ray ray = camera.ScreenPointToRay (Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit)){
            return hit.point;
        }
        return Vector3.zero;
        //Using Vector3.zero cuased it to glitch look at 0,0,0.
    }
}

BUT, The wall is scaling the distance between the start position and end position. And I do not have one object as one wall. So I thought i will create more same object adjacently than one scaled object…
My idea was something like this:

        float dist = Vector3.Distance (start.transform.position, end.transform.position);
        float Count = dist / WallPrefab.GetComponent<Renderer>().bounds.size.z;
        for(int i = 0; i <= Count; i++){
            Wall[i] = (GameObject)Instantiate(WallPrefab, start.transform.position, Quaternion.identity);
            i = i++;
        }

Here I have a mistake because the cycle is repeated to infinity… I feel stupid but it should not stop when the cycle is to achieve a greater value than the “Count”?

i = i++; should just be i++; to increment, normally, but even that isn’t necessary in a “for” loop because i++ is built into the loop definition. Just remove that line entirely.

I tried it as the first but this is not the problem… still infinity

The problem isn’t in your loop logic (well, there is a problem- you still need to delete that i++ line completely), but it’s in the way that you calculate the “Count” variable I think. If you do a Debug.Log() of the value of “Count”, you’ll see that it’s probably some ridiculously massive number. You should also probably have a translate in there so that the initial position is offset by the Z width multiplied by the loop counter, though that’s a different issue.

Also I tried Debug value is around 10

There really doesn’t appear to be any problems with that piece of script then- where are you running this script from? If you run it in Update() it’ll get run multiple times per frame, which will make it look like the loop is running infinitely.

1 Like

umm code is in AddjustWall();
I just looked at it in Update i have only GetInput (); but i didnt noticed AddjustWall() i have in Addjust(); and Addjust() i have in GetInput… So… yea i didint noticed… it running every frame