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 ?
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?
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 () {
}
}
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();
}
}
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;
}*/
}
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?
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 () {
}
}
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