Regarding the case where the color change process (material.color) in Update() is not handled correctly.

Asked 1 years ago, Updated 1 years ago, 36 views

I have asked the same question before, but it has not been resolved and I will ask you again.

I'm a beginner at Unity.

The content is to mass-produce cube objects in for minutes and place them like squares
I'm trying to change the color of some cube objects.
The process of changing colors in Update() is still going through.

However, if you do any other debug.Log processing, it will be handled correctly

Also, if you retrieve the color and submit it in Log, the information of the changed color will be printed.
However, it hasn't changed on the screen

The source content is as follows:
I will also attach a picture of the execution results.

If you know the cause,
Please teach me.

Thank you for your cooperation.

using UnityEngine;
// using UnityEngine.UI;
using System.Collections;

public class BattleMain:MonoBehavior {

    protected int_sceneTask;
    publicGameObject prefab=null;
    public GameObject[,] pass = new GameObject[15,15];
    intisk;
    intjsk;
    // Use this for initialization
    void Start() {
        // Specify the object from which to place it 
        GameObject stageObject=GameObject.FindWithTag("Stage");
        //// tile arrangement
        for(inti=0;i<15;i++){
            for(int j=0;j<15;j++){


                Vector3tile_pos = new Vector3(
                    0+prefab.transform.localScale.x*i,
                    0,
                    0+prefab.transform.localScale.z*j

                    );

                if(prefab!=null){
                    // prefabricated replication 
                    GameObject instant_object = Instantiate(prefab, tile_pos, Quaternion.identity) as GameObject;
                    // attach a replicated prefabricated under the generator 
                    instant_object.transform.parent=stageObject.transform;
                    pass[i,j] = instant_object;
                }
            }
        }
        int Event=Random.Range(4,7); // Install event mass at random
        for(intp=0;p<Ivent;p++){
        intiRandNum=Random.Range(0,14);
        int jRandNum = Random.Range(0,14);
        mass[iRandNum,jRandNum].GetComponent<Render>().material.color=newColor(0.2f,0.8f,0.9f,0.5f);
        }
    }


    // Update is called once per frame

    void Update() {

        mass[5,5].GetComponent<Render>().material.color=newColor(0.0f,0.0f,0.0f);
        Debug.Log("The color of the mass [5,5] that should be black is "+pass [5,5].GetComponent<Render>().material.color);
        class[1,5].GetComponent<Render>().material.color=newColor(0.2f, 0.8f, 0.9f, 0.5f);
        Debug.Log("The color of the mass [1,5] that should be blue is "+pass[1,5].GetComponent<Render>().material.color);
    }


}

·Before execution
Pre-Run

·After execution
Run Results

unity3d

2022-09-30 15:46

1 Answers

I solved myself.
There was a mistake in configuring plefab in the script to be set to the object.


2022-09-30 15:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.