Question by
Vincent032 · Oct 19, 2016 at 08:12 AM ·
c#listanother script
Changing a value in a C# script in a list Objects
My main script:
public class Take_Cover : MonoBehaviour {
public float heath = 100f;
public float rangecover = 3f;
public GameObject[] cov;
public GameObject cov_target = null;
void searchtarget()
{
cov = GameObject.FindGameObjectsWithTag("Cover");
if ( cov.Length > 0 && cov_target == null)
{
foreach (GameObject co in cov)
if ( in this, i wanna check value "cover" in co's Rock script and if it's true)
{
cov_target = co;
set value "cover" in co's Rock script to false;
}
}
}
And Rock script:
public class Rock : MonoBehaviour {
public bool cover = true;
Thank!
Comment
Best Answer
Answer by SmomoGame · Oct 19, 2016 at 08:28 AM
if(co.GetComponent<Rock>().cover){
cov_target = co ;
co.GetComponent<Rock>().cover = false ;
}
use
GetComponent<YourScript>()
to get the reference and do whatever you want directly.
Also, you can save it in a variable first
Rock r = co.GetComponent<Rock>() ;
if(r.cover){
cov_target = co ;
r.cover = false ;
}
Your answer
Follow this Question
Related Questions
Removing items from list by OnTriggerExit 2 Answers
C# list type not working 2 Answers
LoadScene not working 1 Answer
List instance is always the same as the blueprint ? 0 Answers
How Can I Download Multiple Files With Unity Web Request? 0 Answers