I have prefabs that should be referencing different GameObjects, but they all refer to the same one.
In my game I have a simple prefab of a cube. On this cube I have a Script called "Lore". And a box collider 3x the size of the cube that I have made into a trigger and given the tag "Lore". That is all. Underneath my cube prefab (as a child) I have created a Canvas called "Message". So every single Cube prefab has a collider, Lore script. and a Canvas called Message.
In my PlayerMovement script I have created an OnTriggerStay() so that when I walk into the "Lore" trigger it calls upon another function. Like so:
private void OnTriggerStay(Collider other)
{
if (other.tag == "Lore")
{
Debug.LogError("Inside Letter Box");
FindObjectOfType<Lore>().ClickEToPickUpLetter();
}
}
Therefore the entie time I am inside of the "Lore" Trigger it is recalling FindObjectOfType().ClickEToPickUpLetter(); Over an Over.
The function it is calling looks like this.
public GameObject loreMessage;
public void ClickEToPickUpLetter()
{
if (Input.GetKeyDown("e"))
{
loreMessage.SetActive(true);
}
}
In this case. While I am in the trigger, if The user Clicks "e" my GameObject loreMessage is being set to active. In my inspector I referenced the CANVAS that I made as my loreMessage. So that every time the user clicks "e" my Message Canvas is being set to active. here is my problem. When i walk into the trigger "Lore" I have a Debug.Log that says "Inside Letter Box" so i know that my trigger is working. I have also put a Debug.Log into my ClickEToPickUpLetter() Function. So i know that function is being called aswell when the user clicks "e". My problem is that the GameObject that is being turned on (In my case loreMessage, as in my Canvas) is the same canvas every single time.
I have this prefb placed over multiple parts of the map. And each object has a lore script with the same collider. The only difference is that on each object i have referenced a different canvas, so that when the user clicks "e" a new canvas opens up , dislaying a new image. Why is the same canvas being referenced every time the script is called even though each individual script has reference to a different canvas/GameObject. is this because it is a prefab? All i am trying to accomplish is simply walking up to a trigger. In this case "Lore" being able to press "e" on top of it. And when the user presses the button a new canvas is being opened. every time they enter a new collider. But it doesnt seem to be opening the game object i have referenced in the inspector. Instead it opens the first canvas i originally created every time. Thanks any answers are much appreciated.
Your answer
Follow this Question
Related Questions
Cannot figure out GetComponent for the life of me 1 Answer
GameObject SetActive not reactivating 5 Answers
Scrpits component disappears when I enter playmode 0 Answers
Is there a way to refer to the only script the object has without naming the script? 2 Answers
How do I fix my code so that these If statements turn an object off or on depending on a number 2 Answers