how to change color of gameobject on list

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

public class test : MonoBehaviour {

[B]    public List<GameObject> Bloques = new List<GameObject>();
    static System.Random list = new System.Random();[/B]

    void Awake()
    {
        int x = list.Next(9);
    }
    void Update()
    {
         

        if (Input.GetMouseButtonDown(0)) {
            int x = list.Next(9);
            switch (x)
        {
            case 0:
 
                    GameObject.FindWithTag("Base1");


                    print("0");
                    break;
            case 1:
                    print("1");
     
                    break;
            case 2:
                    print("2");
         
                    break;
            case 3:
                    print("3");
           
                    break;
            case 4:
                Debug.Log("4");
       
                    break;
            case 5:
                Debug.Log("5");
       
                    break;
            case 6:
                Debug.Log("6");
       
                    break;
            case 7:
                Debug.Log("7");
       
                    break;
            case 8:
                Debug.Log("8");
           
                    break;
        }
        }
    }


 

     

    /*public GameObject GetGameObject_FromBloques(string name)
    {
        foreach (GameObject gameObject in Bloques)
        {
            if (gameObject.name == name)
            {
                return gameObject;
            }
        }
        Debug.LogError("Game Object not found in Bloques!");
        return null;
        }*/
}

Hey there. Post #135 and still not using code tags :frowning: Opening multiple threads, not saying when they are completed at least half of the time… sigh.

Bloques[x]. // etc.. don't even need a switch here.

yes i need xdd

Okay, well you changed the post (glad you finally got the tags working).
From which list are you trying to change the colour of the game object ?
The Bloques list or something else?

yup the bloques list but idk how to use it because ona rrays should be bloques[0]; (for ex) but here dont work and i didnt find anything :frowning:

Do you fill the List in the inspector?
Lists can be indexed just like arrays, provided that the index is valid.

sure…

   public Color naranja;
    public Color basen;

    void Awake()
    {
        int x = list.Next(9);
    }
    void Update()
    {
          

        if (Input.GetMouseButtonDown(0)) {
            int x = list.Next(9);
            switch (x)
        {
            case 0:
                    GameObject.FindWithTag("Base1");
                    gameObject.GetComponent<Renderer>().material.color = naranja;

i try this dont work :confused:
error on unirt
There is no ‘Renderer’ attached to the “GameObject” game object, but a script is trying to access it.
You probably need to add a Renderer to the game object “GameObject”. Or your script needs to check if the component is attached before using it.
test.Update () (at Assets/test.cs:26)

Here, my best suggestion would be:


Get a little more comfortable with the basics of Unity + scripting and you won’t be trapped on such simple issues as you make your own games/projects. Good luck :slight_smile:

first i speak spaanish then if u want a good learn u have to pay and they say “Scripting is a skill that takes some time and effort to learn. The intention of this section is not to teach you how to write script code from scratch, but rather to explain the main concepts that apply to scripting in Unity.”

Don’t know what you mean about paying for the tutorials. And I realize that it’s not the be all, end all of programming (on the Unity site), but you have many small issues because you’re starting out (I’ve been there, too).

For instance, you’re calling a GameObject.Find without assigning the return value.
You’re looking up a component on the (current) game object, instead of the one in the list (or the found that you presumably tried to find in the previous line), and also you’re switching on a list (index) but not using that result…

I’m only trying to be helpful.

https://certification.unity.com/courseware?_ga=2.41469922.1183340541.1494522890-1334611134.1490470822

Okay, I wasn’t talking about that part, though.

Anyways, if you want don’t want to do the tutorials, that’s your choice; as I said, I was only offering a suggestion & trying to be helpful.

Did the things I pointed out help you at all to understand what’s wrong/how to fix it?

what tutorials xD? only return to here https://docs.unity3d.com/ScriptReference/index.html and give u all the api no videos or exercise i think

I was referring to:
https://unity3d.com/learn/tutorials
and: https://unity3d.com/learn/live-training

Of course the manual and API are good, too :slight_smile:

yea but like i say i speak spanish xdd anyway i cant do it yet :/, i am trayng all for change the color of object [0]

Bloques[x].GetComponent<Renderer>().material.color = naranja

for learn u use only learn from unity ? i mean if u know some channel on yt pls share jaja and ty!! it work

1 Like

For me, personally, I’ve studied programming in general on and off for years (c and C#). When I came to Unity, I tried to learn about the methods, API, manual and how to use the editor. I’m still learning, too :slight_smile:
But yes, I’ve watched some tutorials and practiced. :slight_smile: