…a quick and most likely very dumb question (which I can’t seem to figure out):
How would I move (or do anything actually) an object in the scene, after previously putting that object in an array? What I mean is:
I have 100 apple objects in my scene
I create an array in a script and fill it with apples from the scene(at runtime)
I want to select the apple, which is at 47th (or any position for that matter) in the array and move it (or do anything else)
Any ideas on how to best do it?
…I know that I can find objects in the scene using the GameObject.FindGameObjectsWithTag(“Apple”) and put them into another array… but that just doesn’t connect in my mind to how would I actually use it to do what I wish…
once you have the object in the array, all you should need to do is ArrayName[47].transform.Translate() or whatever script you have to control the object
Thanks for your reply… that seems straightforward, but I can seem to be able to do it still… I’m getting an error
Here’s what I have to do with that moving an object from an array:
GameObject[] applesArray = GameObject.FindGameObjectsWithTag("Fruits");
//Go through the array and delete all objects other then apples
for (int i = 0; i < applesArray.Length; i++)
{
if (applesArray[i].gameObject.name != "Apple")
{
applesArray[i] = null; //remove all of the fruits, except apples
}
}
if (bTemp == true) //disregard this - stops the following few statements from executing multiple times
{
bTemp = false; //again, disregard this
Debug.Log("applesArray.length (apples only)= " + applesArray.Length); //output of this confirms size of array = 2
applesArray[0].transform.Translate(5, 5, 5, Space.World); //causes the error
}
I also know that there are at least 2 apples in the scene, meaning that applesArray[0] in the last if statement is guaranteed to have an apple at index 0…
…but when I do that, I get an:
NullReferenceException: Object reference not set to an instance of an object
Apple.move() …etc
Now that’s what I don’t understand - if I get all of the objects from the scene and fill an array with references to those objects, why then, is the reference at index 0 is “not set to an instance of an object”…
When you set an entry to null here:
applesArray = null; In this case, the entry happens to be entry 0 Later on you try to access that same entry applesArray[0].transform.Translate(5, 5, 5, Space.World); It returns a null value, so you’re essentially doing this: null.transform.Translate(5, 5, 5, Space.World); That throws the NullReferenceException. If you’re going to be adding/removing items from the array, then you should use the List class as it will do resizing for you automatically: ```
*List apples = new List(GameObject.FindGameObjectsWithTag(“Fruits”));
//Go through the array and delete all objects other then apples
for (int i = apples.Count; i >= 0; i–)
{
if (apples[i].gameObject.name != “Apple”)
{
apples.RemoveAt(i); //remove all of the fruits, except apples
}
}
if (bTemp == true) //disregard this - stops the following few statements from executing multiple times
{
bTemp = false; //again, disregard this
Debug.Log("apples.Count (apples only)= " + apples.Count); //output of this confirms size of array = 2
if (apples.Count > 00
{
apples[0].transform.Translate(5, 5, 5, Space.World); //causes the error
}
else
{
//there are no apples! Do something else?
}
}* ``` You’ll note that I do the loop to remove apples in reverse order. This is necessary with a for loop because if you go forward, as you remove items all the indexes get shifted so you end up skipping entries.
Heh, oh yeah, totally forgot about that method and lambdas; you can tell how often I have to do that (usually I filter items before they go in my list :-P)
I get it now! - the core error, as it works out, was a simple mistake of not attaching the “Fruit” tags to the apple objects - that’s what was breaking the whole thing… But again thanks for the tips
Now 1 last question:
Every apple in the scene has a script attached to it. Each script has a boolean, which is causing the move function to execute. The move function move the stem of the apple (another object in the apple prefab).
What I would like to do now, is to (in Inspector view, in-game) enable that boolean, which would cause that particular apple’s stem (the one, that has the script attached to it, whose boolean was just changed) to move…
Is there a way of finding a particular prefab, to which a certain script instance is attached to? And then finding a particular object from that prefab?
after some thought, I figured that a much simpler way of asking that question is:
How can I find a particular child of an object?
and
How can I find an object at a specific location? (Figured, that I can (theoretically) create a sphere at a point in space and then get the object, closest to the centre… but that won’t work, because smaller objects can be closer to the centre, than the big ones, even though their shape is further away if you know what I mean…)