- Home /
GUI Selection c# giving prioity to which ever gameobject has script attached first?
so i have a GUI Selection and it "mostly" works except for when i select "gameobject2" and use that and then select "gameobject1" in the gui and try to use that. it will instead use "gameobject2" since i added that script before the other one. if i select the "gameobject1" and use it right off the bat it works but then select "gameobject2" use it then go back to the "gameobject1" it will use "gameobject2". i don't think it has anything wrong with my code although you can take a look.
Note Class2 is the script attached to gameobject 1 and 2 otherClassPrefab is assigned to 2 different things on gameobject 1 and 2
` public class Class2: MonoBehaviour { [SerializeField] OtherClass otherClassPrefab; //selected state bool selected = false; { var images = FindObjectsOfType<Class2>(); foreach (Class2 image in images) { images.GetComponent<SpriteRenderer>(); } selected = true; Debug.Log(otherClassPrefab); } public OtherClass SetSelectedOtherClass() { if (selected) return otherClassPrefab; return null; } } public class Class1 : MonoBehaviour { OtherClass otherclass; private void OnMouseDown() { Class2[] class2Buttons = FindObjectsOfType(); foreach (Class2 class2Button in class2Buttons) { if (class2Button.SetSelectedOtherClass() != null) { otherclass = class2Button.SetSelectedOtherClass(); } } //do instantiate method } private void InstantiateMethod(Vector2 randomvector2) { if (!otherclass) return; Debug.Log(otherclass); OtherClass newOtherClass = Instantiate(otherclass, randomvector2, Quaternion.identity); } }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I add a speed factor to the script ? 0 Answers
how to keep track of Lean Tween function? 0 Answers
How can i check and fire an event when the user look at specific object ? 0 Answers