How to properly access an object in an array using a for loop?
So im spawning in multiple clones of an object(a wood plank) to build a structure. The issue is when I try to rotate one instance of those clones, it rotates all instances at once. Ive tried to fix this issue by storing all instances into a gameObject array and then test which instance is closest to my touch position to only move that object. Ive troubleshooted everything and the problem is getting access to each instance in the for loop here.
//Get the vector position of the touch
Vector2 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//Temporarily set the closet instance to the first instance of the array
smallest = Vector2.Distance(ToolSpawner.Clone[0].transform.position, clickedPosition);
//run a loop to check all instances and see which one is closet to the touch position
for (int i = 0; i < ToolSpawner.Clone.Length; i++)
{
objPos = ToolSpawner.Clone[i].transform.position; <--------HERE IS THE LINE WITH AN ISSUE.
dist = Vector2.Distance(objPos, clickedPosition);
if (dist < smallest)
{
smallest = dist;
d = I; // d is the variable to hold the number instance closest to the touch so I can later access that instance
}
}
I am getting access to this array from another class called "ToolSpawner". The issue is definitely when I use the variable "I" because when I type in a set value it will work just fine. If you could help me out this would be great, thanks!
Your answer
Follow this Question
Related Questions
Trying to program two buttons to appear when the player in my game dies 0 Answers
Delete GameObject on a specific Grid Location 1 Answer
I want to change the reference axis of an object relative to the camera of another object 0 Answers
SetActive() not working on pooled objects? 2 Answers
Euler angles in inspector 3 Answers