Search Unity

How to Find 2th Gameobjects with the same names?

Discussion in 'Scripting' started by Slyrfecso1, Dec 30, 2015.

  1. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Hi,

    I have save-load function in my project. I can load the saved strings from MySQL with PHP. The only problem, if I have 2 or more Gameobjects with the same name then I can't load the right datas for 2th or more Gameobjects. Only the first object receive datas.

    I can't insert the for in foreach, because that is multiplied.

    Code (CSharp):
    1. //THIS IS INSERTING THE SAVED GO.
    2. for (int a = 0; a < loadNames1Split.Length-1; a++)
    3. {
    4.                                
    5. GameObject myPrefab = Resources.Load("Prefabs/"+loadNames1Split[a]) as GameObject;
    6. GameObject spawnedObject = Instantiate<GameObject>(myPrefab);
    7. spawnedObject.transform.position = new Vector3(float.Parse(loadPosx1Split[a].ToString()), float.Parse(loadPosy1Split[a].ToString()), float.Parse(loadPosz1Split[a].ToString()));
    8. spawnedObject.transform.rotation = Quaternion.Euler(spawnedObject.transform.rotation.x, float.Parse(loadRoty1Split[a].ToString()) , spawnedObject.transform.rotation.z);
    9. spawnedObject.name = spawnedObject.name.Replace("(Clone)","").Trim();
    10. }
    11.                        
    12.                            
    13.                            
    14. //THIS IS CHANGING THE TEXTURE ON INSERTED GO.
    15. for (int b = 0; b < loadButorlapokNevek1Split.Length-1; b++)
    16. {
    17. GameObject FindButorlapok = GameObject.Find(loadButorlapokNevek1Split[b]);
    18. FindButorlapok.GetComponent<Renderer>().material.mainTexture = Resources.Load("Materials/"+loadButorlapokSzinek1Split[b]) as Texture;
    19. }
    20.  

    If you need anything please write to me!
    Any idea will be helpful.
     
    Last edited: Dec 30, 2015
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,182
    If you tag them you could use FindGameobjectWithTag as that returns an array of objects. I don't think Find has the option of returning an array of objects.
     
  3. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    I tried with Tags, but the problem is, all Tags have unique array.
    How can I add different FOR cycle for tags in FOR cycle without multiple?

    If I use [a] index for all Tags, then it is wrong because:

    loadNames1Split = 6 elements
    loadButorlapokSzinek1Split= 7 elements
    loadFemfestekekSzinek1 = 17 elements
    loadKarpitokSzinek1 = 2 elements



    Code (CSharp):
    1. for (int a = 0; a < loadNames1Split.Length-1; a++)
    2. {
    3. GameObject myPrefab = Resources.Load("Prefabs/"+loadNames1Split[a]) as GameObject;
    4. GameObject spawnedObject = Instantiate<GameObject>(myPrefab);
    5. spawnedObject.transform.position = new Vector3(float.Parse(loadPosx1Split[a].ToString()), float.Parse(loadPosy1Split[a].ToString()), float.Parse(loadPosz1Split[a].ToString()));
    6. spawnedObject.transform.rotation = Quaternion.Euler(spawnedObject.transform.rotation.x, float.Parse(loadRoty1Split[a].ToString()) , spawnedObject.transform.rotation.z);
    7. spawnedObject.name = spawnedObject.name.Replace("(Clone)","").Trim();
    8.                            
    9. Renderer[] spawnedChilds = spawnedObject.GetComponentsInChildren<Renderer>();
    10. foreach (Renderer sChComponent in spawnedChilds)
    11. {
    12. if(sChComponent.tag == "Butorlap Objects List")
    13. {
    14. sChComponent.GetComponent<Renderer>().material.mainTexture = Resources.Load("Materials/"+loadButorlapokSzinek1Split[a]) as Texture;
    15. }
    16. if(sChComponent.tag == "Femfestek Objects List")
    17. {
    18. sChComponent.GetComponent<Renderer>().material = Resources.Load("Materials/"+loadFemfestekekSzinek1Split[a]) as Material;
    19. }
    20. if(sChComponent.tag == "Szovet Objects List")
    21. {
    22. sChComponent.GetComponent<Renderer>().material = Resources.Load("Materials/"+loadKarpitokSzinek1Split[a]) as Material;
    23. }
    24. }
    25. }
     
    Last edited: Dec 30, 2015
  4. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    This solved, thanks for idea. ;)

    Code (CSharp):
    1.  
    2. if(sChComponent.tag == "Butorlap Objects List")
    3. {
    4. for (int b = 0; b < loadButorlapokNevek1Split.Length-1; b++)
    5. {
    6. if(sChComponent.name == loadButorlapokNevek1Split[b])
    7. {
    8. //Debug.Log(loadButorlapokNevek1Split[b] + "  -  " + loadButorlapokSzinek1Split[b]);
    9. sChComponent.GetComponent<Renderer>().material.mainTexture = Resources.Load("Materials/"+loadButorlapokSzinek1Split[b]) as Texture;
    10. }
    11. }
    12. }
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,056
    (Insert my usual rant against GameObject.Find <here>)

    If I understand correctly, you're trying to find objects by name among the objects you've just spawned, correct? You could track objects as you spawn them.
    (Add 'using System.Collections.Generic' to beginning of your script file)
    Code (csharp):
    1. class NameObjectList {
    2. public List<GameObject> objects;
    3. }
    4. Dictionary<string, NameObjectList> myDictionary = new Dictionary<string, NameObjectList>();
    5. ...
    6. //within for (...loadNames1Split...), probably at the end
    7. if (myDictionary.ContainsKey(spawnedObject.name) ) {
    8. myDictionary[spawnedObject.name].objects.Add(spawnedObject);
    9. }
    10. else {
    11. NameObjectList newNOL = new NameObjectList();
    12. newNOL.objects = new List<GameObjects>();
    13. newNOL.Add(spawnedObject);
    14. myDictionary.Add(spawnedObject.name, newNOL);
    15. }
    16.  
    17.  
    18. int x=0;
    19. //replacing your GameObject.Find line
    20. List<GameObject> thisList = myDictionary[loadButorlapokNevek1Split[b] ];
    21. GameObjectFindButorlapok = thisList[x];
    22. x++;
    Or something like that. That was written on-the-fly and is untested, so it may not be 100%?
     
  6. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    I like your solution, thank you so much.:cool::cool::cool::cool::cool::cool:
    Happy new Year.

    If you have time visit my first WebGL project.
    http://terberendezo.alexbutor.hu



    I've changed from Dictionary to List.
    Code (CSharp):
    1. public List<GameObject> ButorlapokList = new List<GameObject>();
    2. public List<GameObject> FemfestekekList = new List<GameObject>();
    3. public List<GameObject> KarpitokList = new List<GameObject>();
    4.  
    5. ...
    6. //CLEARING LIST BEFORE INSERTING
    7. ButorlapokList = new List<GameObject>();
    8. FemfestekekList = new List<GameObject>();
    9. KarpitokList = new List<GameObject>();
    10.  
    11. //PRODUCT INSERTING
    12. for (int a = 0; a < loadNames1Split.Length-1; a++)
    13. {
    14. GameObject myPrefab = Resources.Load("Prefabs/"+loadNames1Split[a]) as GameObject;
    15. GameObject spawnedObject = Instantiate<GameObject>(myPrefab);
    16. spawnedObject.transform.position = new Vector3(float.Parse(loadPosx1Split[a].ToString()), float.Parse(loadPosy1Split[a].ToString()), float.Parse(loadPosz1Split[a].ToString()));
    17. spawnedObject.transform.rotation = Quaternion.Euler(spawnedObject.transform.rotation.x, float.Parse(loadRoty1Split[a].ToString()) , spawnedObject.transform.rotation.z);
    18. spawnedObject.name = spawnedObject.name.Replace("(Clone)","").Trim();
    19.                            
    20. //ADDING CHILDS OF GAMEOBJECTS TO LIST
    21. Renderer[] spawnedChilds = spawnedObject.GetComponentsInChildren<Renderer>();
    22. foreach (Renderer sChComponent in spawnedChilds)
    23. {
    24. if(sChComponent.tag == "Butorlap Objects List")
    25. {
    26. ButorlapokList.Add(sChComponent.gameObject);
    27. }
    28. if(sChComponent.tag == "Femfestek Objects List")
    29. {
    30. FemfestekekList.Add(sChComponent.gameObject);
    31. }
    32. if(sChComponent.tag == "Szovet Objects List")
    33. {
    34. KarpitokList.Add(sChComponent.gameObject);
    35. }
    36. }
    37.  
    38. //CHANGING TEXTURES AND MATERIALS ON LISTS
    39. for (int b = 0; b < loadButorlapokSzinek1Split.Length-1; b++)
    40. {
    41. ButorlapokList[b].GetComponent<Renderer>().material.mainTexture = Resources.Load("Materials/"+loadButorlapokSzinek1Split[b]) as Texture;
    42. }
    43.  
    44. for (int c = 0; c < loadFemfestekekSzinek1Split.Length-1; c++)
    45. {
    46. FemfestekekList[c].GetComponent<Renderer>().material = Resources.Load("Materials/"+loadFemfestekekSzinek1Split[c]) as Material;
    47. }
    48.  
    49. for (int d = 0; d < loadKarpitokSzinek1Split.Length-1; d++)
    50. {
    51. KarpitokList[d].GetComponent<Renderer>().material = Resources.Load("Materials/"+loadKarpitokSzinek1Split[d]) as Material;
    52. }
    53. }
     
    Last edited: Jan 1, 2016
  7. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    unity3d.com/learn
     
unityunity