Changing a certain element from a bool array in another script
Hi folks,
Unity noob here! I have the following Problem, while trying to build a small step sequencer:
I have a public bool array in Script A which consists of 16 elements.
In Script B I have a OnTriggerEnter event, which fires, if one of the 16 buttons (that I build from cubes) is triggered. What I'm trying to achieve is that, if for example I press button 3, the element 3 of the bool array in Script A is changing it's value to true.
I already linked the Script A into Script B via a public GameObject and I am accessing the script via a GetComponent, but I don't know how to access the one element of the array that I need.
Your help is appreciated :)
Answer by Hethu · Aug 09, 2016 at 11:25 PM
I'm not that experienced as well but assuming that you've assigned some values like this in Script A:
//Bool array with 4 entries
bool[] myArray = new bool[]{ true, true, false, true };
//In Script B ..
public void Element3()
{
element3 = ScriptA.myArray[2]; // An Array starts at Index 0, the 3rd Element is Index 2
if (Input.GetKeyDown("3"))
{
if(!element3)
element3 = true;
else
element3 = false;
}
ScriptA.myArray[2] = element3;
}
//In FixedUpdate or Update
void FixedUpdate()
{
Element3();
}
This should do what you want. If not I'm, sorry :( Can't test it right now..
Thanks mate :)
Here's my OnTriggerEnter function:
void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Player" && Time.time >= timestamp) { ActiveON = !ActiveON; Sequencer.GetComponent().sequence[3] = ActiveON; timestamp = Time.time + timebetweenshot; } }
Your answer
Follow this Question
Related Questions
How to get variable by name(string typed in inspector) in array with index from another script? 0 Answers
How to change the wheel.forwardFriction.stiffness value? 1 Answer
Randomized array result from other scipt 2 Answers
Setting slider value over time using C# script? 0 Answers
How do I check if bool is true from another script? 2 Answers