Yes you can. You should store both ifs into a variable in each script locally and then access that variable through the thrid script using GetComponent<>().
@AlmantusK Thank you very much for solution. It work perfect. But now, I have another problem How can I hide/show an object from list? Here;
ScriptOne s1 = gameObject.GetComponent<ScriptOne>();
if(s1.conditionList1 != null)
{
if (hit.transform.gameObject.tag == "Apple" || hit.transform.gameObject.tag == "Car" || hit.transform.gameObject.tag == "Flower") //Apple,car and flower just for example
{
GameObject[] list1;
list1 = GameObject.FindWithTag("Apple").SetActive(false); //ofcourse its not working. This line give me " CS0029: Cannot implicitly convert type `void' to `UnityEngine.GameObject[]' " error.
}
}
@CrymX and @LeftyRighty thank you very much guys!! You are perfect! But I have another problem. I can’t figure out. I try everythnk.
ScriptOne s1 = gameObject.GetComponent<ScriptOne>();
if(s1.conditionList1) // This line give me this error: "NullReferenceException: Object reference not set to an instance of an object"
{
if (hit.transform.gameObject.tag == "Apple")
{
GameObject[] list1;
list1 = GameObject.FindGameObjectsWithTag("Apple");
foreach(GameObject oneApple in list1)
{
oneApple.SetActive(false);
}
gameController.AddScore (scoreValue);
Destroy (hit.transform.gameObject);
}
your gameObject have some component (like a rigidbody, a script, a meshRenderer). If your gameObject don’t have the component ScriptOne, GetComponent return null.
So you can drag and drop your script into your GameObject or Add the componenent ScriptOne at runtime like this :
this requires a “ScriptOne” component to be on the same gameobject as this script… if there isn’t one, s1 is null. You then try to access a property of s1 which causes the error.
and now I get another error I think I can’t fix it.
//ScriptOne
...
void UpdateScore ()
{
scoreText.text = "Score: " + score; // this line. " NullReferenceException: Object reference not set to an instance of an object
}
}
My other scripts works fine and I don’t get Null error but this line/code doesn’t work;
GameController s1 = gameObject.GetComponent<GameController>();
if (s1.conditionList1 != null) // Alert(In Visual Studio): The result of the expression is always true since a value of type 'bool' is never equal to 'null' of type 'bool?' Alert(In Unity): The result of comparing value type `GameController.conditionList1' with null is `true'
Its don’t give me error but its not work. @CrymX and yes. GUIText attached to component via the inspector.