简体   繁体   中英

I have a for loop in C#, which works in conjuction with Unity engine. But how do i make it so it only calls the loop once?

To give some context: In unity I have 2 boxes, which are both tagged "box" One box is on a plane and the other in the air, when the game is played on box falls on the other. Here is the console for the engine:

Material 1 (UnityEngine.Material)
UnityEngine.Debug:Log(Object)
colourChangeArray:OnCollisionEnter(Collision) (at Assets/colourChangeArray.cs:28)

With material 1 changing to material 2, 3, 4 to 5 and then doing the 1-5 cycle 10-20 times

Below is the code i'm using

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class colourChangeArray : MonoBehaviour
{
    public int trigger = 1;
    public Material[] material;
    Renderer rend;

    // Use this for initialization
    void Start()
    {
        rend = GetComponent <Renderer> ();
        rend.enabled = true;
        rend.sharedMaterial = material[0];

    }


    // Update is called on collision
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "box")
        {

            for(int i = 0; i <material.Length; i++){
                rend.sharedMaterial = material[i];
                Debug.Log(rend.sharedMaterial);
                    }
        }
        else
        {
            rend.sharedMaterial = material[0];
            Debug.Log(rend.sharedMaterial);
        }

    }
}

The cycle between materials happen a few times because of the internal bounciness of the objects in unity. You can decrease it somehow (which I don't remember) but, I had a similar case once and no matter how much you decrease that it happens. So the best approach would be to store a value indicating it has been done before like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class colourChangeArray : MonoBehaviour
{
    public int trigger = 1;
    public Material[] material;
    Renderer rend;
    private bool changeMaterialDone;

    // Use this for initialization
    void Start()
    {
        rend = GetComponent <Renderer> ();
        rend.enabled = true;
        rend.sharedMaterial = material[0];
        changeMaterialDone = false;
    }


    // Update is called on collision
    void OnCollisionEnter(Collision col)
    {
        if (!changeMaterialDone && col.gameObject.tag == "box")
        {
            changeMaterialDone = true;
            for(int i = 0; i <material.Length; i++)
            {
                rend.sharedMaterial = material[i];
                Debug.Log(rend.sharedMaterial);
            }
        }
        else
        {
            rend.sharedMaterial = material[0];
            Debug.Log(rend.sharedMaterial);
        }
    }
}

I hope it helps you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM