Hello, it’s my first topic, I’m french (and have a poor english grammar) and before write this one, I did a lot of research during many days, I create game from many years with Unity.
Let me explain my problem.
I have one Gameobject with two same Script (Goal), these script “Goal” is like a “Quest” who need to be completed.
They are never enable at the same time, first one is enabled and when this first one is completed by player, second one is enable (for more info both have boolean “isCompleted”).
My player check with raycast, and tag, gameobject in front of him and take the enabled script.
The player can complete the “Goal” and put boolean isCompleted at True.
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hitTask, 2f))
{
if (hitTask.collider.CompareTag("Goal"))
{
if (goalTask)
goalTask = null;
Goal[] goalTasks = hitTask.transform.GetComponents<Goal>();
foreach (Goal task in goalTasks)
{
goalTask = null;
if (task.isActiveAndEnabled && !task.IsCompleted)
{
goalTask = task;
break;
}
else
goalTask = null;
}
}
}
And if the player presses the E key, it calls the CompleteGoal() method of this Goal script.
When he presse E key we made new check of activation on script.
if (Input.GetKeyDown(KeyCode.E) && goalTask != null && !goalTask.IsCompleted && goalTask.isActiveAndEnabled)
goalTask.CompleteGoal();
BUT ! Here is my issue, even though my second script is activated and isCompleted is set to false, it’s the (bad) CompleteGoal() method of the first (and disabled) “Goal” script is called up.
I have try with :
goalTask.isActiveAndEnabled
but also just with :
goalTask.enabled
And I have try to put my Input E key condition IN the raycast function :
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hitTask, 2f))
{
if (hitTask.collider.CompareTag("Goal"))
{
if (goalTask)
goalTask = null;
Goal[] goalTasks = hitTask.transform.GetComponents<Goal>();
foreach (Goal task in goalTasks)
{
goalTask = null;
if (task.isActiveAndEnabled && !task.IsCompleted)
{
goalTask = task;
if (Input.GetKeyDown(KeyCode.E) && goalTask != null && !goalTask.IsCompleted && goalTask.isActiveAndEnabled)
goalTask.CompleteGoal();
break;
}
else
goalTask = null;
}
}
}
I have the same result, only the first script is called up even if it is deactivated and his boolean “isCompleted” is false.
Did a make something wrong in my code ?
Thank you for your diagnoses and your patient.