Help Making a static function

i need a static function but i cant beacause i have variables that arent static, but if i put on static i cant do the list or other thing some alternative or solution ? :frowning:

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

public class test : MonoBehaviour {

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


    public Color naranja;
    public  Color basen;


    public static void  Change () {

        if (Cubos.color == false)
        {

            int x = list.Next(9);
            Bloques[x].GetComponent<Renderer>().material.color = basen;

        }
    }
  


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

        if (Input.GetMouseButtonDown(0)) {
            int x = list.Next(9);
            switch (x)
        {
            case 0:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                    }
                    break;
            case 1:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 2:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 3:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 4:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 5:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 6:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 7:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 8:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false )
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    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;
        }*/
}

Why is it you think you need a static function? Static functions have no access to instance variables, they exist entirely independently of any instances of that class. Why is it that you think Change needs to be static?

because i need in another script

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

public class Cubos : MonoBehaviour {
    public static bool live;
    public static bool color;
    // Use this for initialization
    public void Click()
    {
        if (Input.GetMouseButtonDown(0)) {

            color = false;
            test.Change();
    }
    }

    void Update () {
       
    }
}

why there is another way for use is ?

wtf always i do a new post they told me stop abusing of tags and i only use visual studio c sharp and error on script… seriusly ?
mod can u tell me why this is not a error on script .-.

What you really need is a reference to that object from the second object. A static method will not help you here. As you can see, if you have a reference to an instance of another class you can call public methods on it. B is not related to A in any way but it holds a reference to an instance of A, and can call methods on that instance.

class A : MonoBehaviour {
  public void DoSomething() {  }
}

class B : MonoBehaviour {
  public A a;

  void OnEnable() {
    a.DoSomething();
  }
}

so u mean made a new script ?

but now i have all error on script because i made the reference of variables on another script

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

public class Acceso : MonoBehaviour
{

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


    public Color naranja;
    public Color basen;

    public void Change()
    {

        if (Cubos.color == false)
        {

            int x = list.Next(9);
            Bloques[x].GetComponent<Renderer>().material.color = basen;

        }
    }

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

public class test : MonoBehaviour {

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


   // public Color naranja;
//    public Color basen;

  public Acceso a;

    void OnEnable()
    {
        a.Change();
    }


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

        if (Input.GetMouseButtonDown(0)) {
            int x = list.Next(9);
            switch (x)
        {
            case 0:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                    }
                    break;
            case 1:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 2:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 3:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 4:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 5:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 6:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 7:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false)
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    break;
            case 8:
                    Bloques[x].GetComponent<Renderer>().material.color = naranja;

                    if (Cubos.live == true && Bloques[x].GetComponent<Renderer>().material.color == naranja)
                    {
                        if (Cubos.color == false )
                        {
                            Bloques[x].GetComponent<Renderer>().material.color = basen;

                        }
                    }
                    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;
        }*/
}

error on “bloques” “basen” “naranja” “list”

naranja, in your updated script would be: a.naranja

NullReferenceException: Object reference not set to an instance of an object
test.Awake () (at Assets/test.cs:26)
what mean ? code

    void OnEnable()
    {
        a.Change();
    }


void Awake()
    {

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

Well, your ‘list’ variable is static in another class.
2 things are the matter here:

  1. it’s not public, so you cannot access it.
  2. when it’s static, you have to call it with the class, not the instance variable (not ‘a’ here). It would be: Accesso.list

but is not static…

    public List<GameObject> Bloques = new List<GameObject>();
     public System.Random list = new System.Random();


    public Color naranja;
    public Color basen;

Ah okay, well you changed that from your previous post. I am not a mind reader lol.
Did you assign the ‘Acceso’ variable in the inspector? If you did, maybe you should move it to Start() instead in case one executes before the other.

i have a public a but i dont know what to put in jajaj
my public
public Acceso a;

accesso scriupt

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Acceso : MonoBehaviour
{

    public List<GameObject> Bloques = new List<GameObject>();
    public System.Random list = new System.Random();


    public Color naranja;
    public Color basen;

    public void Change()
    {

        if (Cubos.color == false)
        {

            int x = list.Next(9);
            Bloques[x].GetComponent<Renderer>().material.color = basen;

        }
    }

}

drag the same object that you showed in the screenshot, since I believe that is what ‘A’ wants - the Acceso script.
Obviously if it’s empty, nothing can happen, right? :slight_smile:

xdd same error
NullReferenceException: Object reference not set to an instance of an object
Cubos.OnEnable () (at Assets/Cubos.cs:12)

cubos script

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

public class Cubos : MonoBehaviour {
    public static bool live;
    public static bool color;
    public Acceso a;
    // Use this for initialization
    void OnEnable()
    {
        a.Change();
    }
    public void Click()
    {
        if (Input.GetMouseButtonDown(0)) {

            color = false;
            a.Change();
    }
    }

    void Update () {
       
    }
}

Well, try to move it to Start(). and see if you still get it.
That’s also not the same error…

Can you click from that script and it works?

my game got crazy xddd look what i did

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

public class test : MonoBehaviour {

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


    // public Color naranja;
    public static bool vivir = false;
//    public Color basen;

  public Acceso a;

    void OnEnable()
    {
        a.Change();
    }


void Awake()
    {

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


        if (Input.GetMouseButtonDown(0)) {
            int x = a.list.Next(9);
            switch (x)
        {
            case 0:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                        }
                    break;
            case 1:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                    }
                    break;
            case 2:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                    }


                    break;
            case 3:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                    }
                    break;
            case 4:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                    }
                    break;
            case 5:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                    }
                    break;
            case 6:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                    }
                    break;
            case 7:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                    }
                    break;
            case 8:
                    a.Bloques[x].GetComponent<Renderer>().material.color = a.naranja;
                    if (a.Bloques[x].GetComponent<Renderer>().material.color == a.naranja)
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            a.Change();
                        }
                    }
                    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;
        }*/
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Acceso : MonoBehaviour
{

    public List<GameObject> Bloques = new List<GameObject>();
    public System.Random list = new System.Random();


    public Color naranja;
    public Color basen;

    public void Change()
    {

        if (Cubos.color == false)
        {

            int x = list.Next(9);
            Bloques[x].GetComponent<Renderer>().material.color = basen;

        }
    }

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

public class Cubos : MonoBehaviour {
    public static bool live;
    public static bool color;
    public Acceso a;
    // Use this for initialization

    public void Click()
    {
            color = false;
            a.Change();
    }

    void Update () {
       
    }
}

now when u click 2 change color idk why and do crazy thing jaja

I just have no idea what you mean, sorry :slight_smile:

let me make some sprite and show u xdd

https://gyazo.com/df00c1d05afd17807617e5c1a0441268
should only change 1 color of case not 2 per click .-.