Hi everyone,
I will have several gameobjects (say gameobjects Goal1- Goal4) with the aim to interact with each differently in a given order I load using a subjectspecific .txt file.
Now I found a solution:
// in this void, the trialscripts of each object are started in an order based on the input from the inputFile. If the trialCounter exceeds the number of trials, the game is ended.
public void Trials()
{
int[] configValuesArrayTask = GetComponent<readFileTask>().ReadFileTask();
trialCounter = trialCounter + 1;
responseKeyCount = 0;
taskTextKeyCount = 0;
pauseKeyCount = 0;
pulseNumber = 0;
if (trialCounter > numbTrials)
{
end();
}
else
{
if (configValuesArrayTask[0] == trialCounter)
{
defineTaskGoal1.enabled = true;
Goal1.GetComponent<defineTaskGoal1>().StartGoal1();
}
else if (configValuesArrayTask[1] == trialCounter)
{
defineTaskGoal2.enabled = true;
Goal2.GetComponent<defineTaskGoal2>().StartGoal2();
}
else if (configValuesArrayTask[2] == trialCounter)
{
defineTaskGoal3.enabled = true;
Goal3.GetComponent<defineTaskGoal3>().StartGoal3();
}
else if (configValuesArrayTask[3] == trialCounter)
{
defineTaskGoal4.enabled = true;
Goal4.GetComponent<defineTaskGoal4>().StartGoal4();
}
}
}
However, in the final version, I want to have 3 times 24 GameObjects (hence a txt file where 72 columns indicate the order of interaction). So I am trying to automize this using a for loop instead of writing 72 if statements.
This works fine with loading all gameobjects into an array, but I cannot “individualize” the accessing of the scripts on each game object (defineTaskGoalX) and the public voids in each script (StartGoalX())
// in this void, the trialscripts of each object are started in an order based on the input from the inputFile. If the trialCounter exceeds the number of trials, the game is ended.
public void Trials()
{
int[] configValuesArrayTask = GetComponent<readFileTask>().ReadFileTask();
trialCounter = trialCounter + 1;
responseKeyCount = 0;
taskTextKeyCount = 0;
pauseKeyCount = 0;
pulseNumber = 0;
if (trialCounter > numbTrials)
{
end();
}
else
{
GameObject[] goalObjects;
goalObjects = GameObject.FindGameObjectsWithTag("Tag1").OrderBy(go => go.name).ToArray(); ;
foreach (GameObject goalObject in goalObjects)
{
Debug.Log(goalObject);
}
for (var i = 0; i < goalObjects.Length; i++)
{
defineTaskScript = goalObjects_.GetComponent<defineTaskGoal*>();_
if (configValuesArrayTask[0] == trialCounter)
{
defineTaskScript.enabled = true;
goalObjects_.GetComponent<defineTaskGoal*>().StartGoal*();_
}
}
}
}
Stars mark the points where I dont know how to reference them (basically, in other programming languages, “*” would actually help in this case.)