Im trying to make a list with a number of game objects with also a few vector3 for each ones.
Supposedly this can be achieved with the foreach loop but only shows once in the inspector not being linked to each number of gameobjects. How do i do this?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class listTest : MonoBehaviour {
public List<GameObject> myList;
public Vector3 values;
// Use this for initialization
void Start () {
foreach(GameObject myList in myList){
values = new Vector3 (1, 1, 1);
}
}
}
I’m not really sure I understand what you are attempting to do. A generic list is type safe, which means you can only store the same type in it. So in your case, it’s a list of gameobjects. So you can only store gameobjects in the list.
Now, a foreach loop just says to access each element in the list and do something. Normally you would be doing something to the object in the list, but technically you don’t have to. In this case, all you’re doing is setting the values variable to vector3(1,1,1) over and over and over again.
If you are trying to set the position of each gameobject, you’ll need to do
foreach(GameObject myListChild in myList)
{
myListChild.transform.position = Vector3.one;
}
But I’m not sure if that is what you are trying to do or not. I also suggest you don’t name the single element the same as the list when you iterate through it as it can visually confuse some people.
I’m trying to add a list of bones, to each one I am assigning a vector3 or a few I have in another script to limit their rotation, I had that script attached to them individually now I want to put them in just one properly like a master controller for their limit being each one of them different values is this the best way to go?
If your list of gameobjects (the myList) is your bones and you want to position those bones, you’ll need to do what I showed, but you’ll have to position them in some way. If these positions are completely random, a foreach loop may not work for you.
If you have a list of positions in another script, you can access that list and do something with it. If you just want each gameobject “bone” to have a vector3 stored on it, you’ll have to have a script. Maybe called boneData that has a vector3 variable on it that you can store a value in.
The other way is to make a bone class that has the bone gameobject and a vector3 for the position and make a list of that class.
You can also make two list if you really wanted and use a for loop to update the vector3 list as you iterate through the gameobjects.
As you have it now, you are only updating the one variable over and over again.
For each bone I want to add two vector3 that I’ve been working on but each one bone haves different rotation limits like:
foreach(GameObject myListChild in myList)
{
rotationMin; //vector3 from a void rotationLimit
rotationMax;// vector3 from a void rotation limit
}
This can be done with one list or two lists or another solution? Sorry to be a pain but I really need to know what’s the effective way of doing such thing.
List<GameObject> myList;
List<Vector3> minPositions;
List<Vector3> maxPositions;
void Start()
{
minPositions = new List<Vector3>();
maxPositions = new List<Vector3>();
foreach(GameObject myListChild in myList)
{
minPositions.Add(new Vector3.one);
maxPositions.Add(new Vector3.one);
}
}
Method 1 is ok, but not great as you will have to make sure your list always line up. If you delete a bone from the myList, you’ll need to remove the vector positions from the other list. You can then access all list using the same index number. Note that this would set min and max position to vector3(1,1,1) for each bone to start.
List<Bone> bones;
void Start()
{
for(int x = 0; x < myLIst.Count;x++)
foreach(Bone bone in bones)
{
bone.minPositions =new Vector3.one;
bone.maxPositions = new Vector3.one;
}
public class Bone
{
//Can add public GameObject bone; if you want a reference to the gameobject
public Vector3 minPositions;
public Vector3 maxPositions;
}
}
Method 2 allows you to attach a Bone script to each bone gameobject and access those values. Or you could just have the list and set it up so there is a reference to the gameobject in the class itself.
There are other methods as well, but I am at work so I can’t really show all of them.
For the first method “minPositions.add” “using System.Collections.Generic;” doesnt recognizes it. How does the .add work?
For the second method after some fixing, not must usage specially for the counter (if its a list of 4 values it will stop on the third) its haves pretty much the same result as the first method after fixed.
Going to square one:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class listTest : MonoBehaviour {
public List<GameObject> myList;
public List<Vector3> vectorRotation;
public Vector3 minPositions;
public Vector3 maxPositions;
// Use this for initialization
void Start () {
}
void Update () {
foreach(GameObject myListChild in myList){
myListChild.transform.position = minPositions;
}
foreach(Vector3 myVector3 in vectorRotation){
}
}
}
Since for every gameobject added to the myList creates an element 0,1,2,3… and the same goes for the vectorRotation can i get them individually?
Lets say i give the list a size of 3 and i want the second gameobject (element1) to be related to the second vector3 of the vectorRotation list (element1)?
If not, whats the purpose of the list?
.Add is how you put stuff into the list. Sorry it should be capped. Typing stuff straight into the forum sometimes I miss those things, but your script editor usually will catch these and let you fix them.
I don’t understand your question. If you are planning to do multiple list in a manager, then your index will determine what is what.
mylist[0] and vectorRotation[0] for example would just assume they are for each other. So if you remove the gameobject from index 0, you also have to remove the rotation from index 0 so the list stay in sync. This isn’t the best practice. Normally you would create a class so that when you target an instance of the class, the values in it all go together.