How do I get the code to Find whichever object is active with FindObjectOfType?
So I have two players you can be two separate GameObjects with the exact same script attached to them. The problem is that I cant get this to find whichever object is active it only ever finds the first created one.
private Player thePlayer; //the reference to the Player script
void Start(){
thePlayer = FindObjectOfType<Player>();
}
The thing is I can't make thePlayer an cause if do I can't seem to reference anything in the script which is the whole purpose of referencing said script so if someone could tell me how to only find the Active object with this type of thing that would be very helpful.
Answer by doublemax · Oct 31, 2016 at 03:27 PM
If you use FindObjectsOfType() (plural) you get an array of all objects of that type. From there you should be able to find you which one is the active one.
https://docs.unity3d.com/540/Documentation/ScriptReference/Object.FindObjectsOfType.html
That is what I tried however It says that the script can't convert to an array and I don't want it to be an array cause then none of the things that need to be referenced are referenced
Your answer
![](https://koobas.hobune.stream/wayback/20220612095730im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Calling Function from another script not working 1 Answer
Disable a group of scripts based on base class. 0 Answers
Can't figure this out! C# access another class 2 Answers
Multiple PowerUps (One ItemBox) 0 Answers
How would I be able to store a value that can be used later with a function that returns void? 1 Answer