PLEAASEE I NEED HELP!!! How to turn Mesh Renderer of cubes on and off by pushing buttons?
Hi everyone,
Please help me with this. I want to enable and unenable the meshrender of the cubes by pushing "e" and "u" buttons, but it didnt work. Everytime I pushed those button nothing happens. I don't know how to fix it (T..T)
The code is as below. What I did was that I extracted positions and colors of the cubes from a csv file. (I cant upload the csv file, I upload the txt file instead in case that if you need the file to run you can change it back to csv file).
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Transparent : MonoBehaviour
 {
     public TextAsset CSV;
     public GameObject Cube;
     public static bool click = true;
     // Start is called before the first frame update
     void Start()
     {
         string[] myCSV = CSV.text.Split('\n');
         for (int i = 0; i < myCSV.Length; i++)
         {
             // Positions of the Cubes
             GameObject myCube = GameObject.Instantiate(Cube);
             string[] values1 = myCSV[i].Split(',');
             float x = float.Parse(values1[0]); // column 1 of the csvfile
             float y = float.Parse(values1[1]); // column 2 of the csvfile
             float z = float.Parse(values1[2]); // column 3 of the csvfile 
             myCube.transform.position = new Vector3(x, y, z);
 
             // Colors of the Cubes
             float r = float.Parse(values1[3]); // column 4 of the csvfile
             float g = float.Parse(values1[4]); // column 5 of the csvfile
             float b = float.Parse(values1[5]); // column 6 of the csvfile
             Renderer myrend = myCube.GetComponent<Renderer>();
             myrend.material.color = new Color32((byte)r, (byte)g, (byte)b, 255);
 
             //MeshRenderer of the Cubes
             MeshRenderer cubeMeshRenderer = myCube.GetComponent<MeshRenderer>();
             cubeMeshRenderer.enabled = click;
 
         }
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown("e"))
         {
             click = true;
         }
         else if (Input.GetKeyDown("u"))
         {
             click = false;
         }
     }
 }
Answer by Hellium · Apr 14, 2020 at 08:56 AM
  public class Transparent : MonoBehaviour
  {
      public TextAsset CSV;
      public GameObject Cube;
      private Renderer[] cubes;
 
      // Start is called before the first frame update
      void Start()
      {
          string[] myCSV = CSV.text.Split('\n');
          cubes = new Renderer[myCSV.Length];
 
          for (int i = 0; i < myCSV.Length; i++)
          {
              // Positions of the Cubes
              GameObject myCube = GameObject.Instantiate(Cube);
              string[] values1 = myCSV[i].Split(',');
              float x = float.Parse(values1[0]); // column 1 of the csvfile
              float y = float.Parse(values1[1]); // column 2 of the csvfile
              float z = float.Parse(values1[2]); // column 3 of the csvfile 
              myCube.transform.position = new Vector3(x, y, z);
  
              // Colors of the Cubes
              float r = float.Parse(values1[3]); // column 4 of the csvfile
              float g = float.Parse(values1[4]); // column 5 of the csvfile
              float b = float.Parse(values1[5]); // column 6 of the csvfile
              Renderer myrend = myCube.GetComponent<Renderer>();
              myrend.material.color = new Color32((byte)r, (byte)g, (byte)b, 255);
  
              //MeshRenderer of the Cubes
              cubes[i] = myCube.GetComponent<MeshRenderer>();
          }
      }
  
      // Update is called once per frame
      void Update()
      {
          if (Input.GetKeyDown("e"))
          {
              foreach(Renderer cube in cubes)
                 cube.enabled = true ;
          }
          else if (Input.GetKeyDown("u"))
          {
              foreach(Renderer cube in cubes)
                 cube.enabled = false ;
          }
      }
  }
Your answer
 
 
             Follow this Question
Related Questions
accessing materials from skinned mesh renderer 0 Answers
I want my script to wait 2 seconds before continue in a condition, in update, using C# 2 Answers
Simple scripting problem 1 Answer
I can access the field but it wont update the value? 3 Answers
How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                