Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Wrong Transform?

Discussion in 'Scripting' started by jordini345, Apr 21, 2024.

  1. jordini345

    jordini345

    Joined:
    Aug 13, 2020
    Posts:
    3
    Code (CSharp):
    1.     public void DestroyTile()
    2.     {
    3.         //TileX = new List<float>();
    4.         GameManager.Instance.SelectedTiles = new List<GameObject>();
    5.         GameManager.Instance.FirstTile = null;
    6.         GameManager.Instance.SecondeTile = null;
    7.         GameObject[] tObjects = GameObject.FindGameObjectsWithTag("Selected");
    8.         GameObject CTile;
    9.         GameManager.Instance.Arrows = 0;
    10.         for (int i = 0; i < tObjects.Length; i++)
    11.         {
    12.             CTile = tObjects[i];
    13.             Vector3Int currentCell = TileMap.WorldToCell(CTile.transform.position);
    14.             //currentCell.y++;
    15.             //Debug.Log(currentCell);
    16.             if(TileMap.GetTile<Tile>(currentCell) == Brown)
    17.             {
    18.                 TileMap.SetTile(currentCell, null);
    19.                 Game.GetComponent<FillTiles>().tilesnull.Add(currentCell);
    20.             }
    21.             Debug.Log("RY: " + Fade.transform.position.y);
    22.             RandomTile(CTile.transform.position.x, Fade.transform.position.y + i);
    23.             Destroy(CTile.GetComponent<Tiles>().currentArrow);
    24.             Destroy(CTile);
    25.             int tI = i + 1;
    26.             //Debug.Log("I: " + tI + " Object: " + tObjects.Length);
    27.             if (tI == tObjects.Length)
    28.             {
    29.                 //Debug.Log("Y: " + currentCell.y + " X: "+ currentCell.x);
    30.                 Game.GetComponent<FillTiles>().FloodFill(StartTile);
    31.             }
    32.         }
    33.     }
    34.  
    35.  
    36.     public void RandomTile(float RTileX, float RTileY)
    37.     {
    38.         Debug.Log("RTileY: " + RTileY);
    39.         GameObject newTile = Instantiate(tile, new Vector3(RTileX, RTileY, 0), tile.transform.rotation);
    40.         TileN++;
    41.         newTile.name = "Tile" + TileN;
    42.         newTile.GetComponent<Tiles>().ThisTile = newTile;
    43.         newTile.transform.parent = transform;
    44.         Sprite newSprite = characters[Random.Range(0, characters.Count)];
    45.         newTile.GetComponent<SpriteRenderer>().sprite = newSprite;
    46.         //Color color = new Color(255, 255, 255, 0);
    47.         //newTile.GetComponent<SpriteRenderer>().color = color;
    48.         newTile.GetComponent<SpriteRenderer>().enabled = false;
    49.         newTile.GetComponent<SpriteRenderer>().sortingOrder = 1;
    50.     }
    51. }


    I have this Script 3 times in my Level. 2 Times the Fade GameObject is the same and this works fine, but the last time its gives me back the Transform of the other Fade GameObject. I checkt a thousend times and in the inspector is the correct GameObject in the slot.
     
  2. jordini345

    jordini345

    Joined:
    Aug 13, 2020
    Posts:
    3
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,410
    This is usually what happens with way too much GetComponent and FindWithTag type use.

    In general, DO NOT USE Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way(tm) success of accessing things in your game.


    "Stop playing 'Where's GameWaldo' and drag it in already!"

    Here's more interesting ways to connect things together:

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();
     
    Last edited: Apr 21, 2024
    Ryiah likes this.
  4. jordini345

    jordini345

    Joined:
    Aug 13, 2020
    Posts:
    3
    Thanks for the reply, it works now. I now just "save" the transform that i need in the GameObject and give it to the Script.