- Home /
Setting a game object in another script
ok guys I am following a tower defense tutorial and want to made some changes there is a method which place a prefab on a tile and disable the tile
if (creditCount >= costValue[turretIndex])
{
creditCount -= costValue[turretIndex];
Instantiate(turretPrefabs[turretIndex], tile.transform.position, Quaternion.identity);
tile.SetActive(false);
canBuild = false;
aud.PlayOneShot(turretDrop);
}
Now I want to add this disabled tile to a game object in another script so i can eanble it again later so I added this line
turretPrefabs[turretIndex].GetComponent<SellItem>().tile = tile;
but it assign random tile to it and not one i want
please help
Answer by tormentoarmagedoom · Sep 16, 2018 at 03:30 PM
Good day.
It does not assigna random tile. If a tile is assigned to variable "tile", is because you did it. If do that new code at the same method, where your variable "tile" have assigned the tile you want, it will act over that tile. if you are doing this in another script, you should know what tile is assigned to variable "tile"...
Bye.
Answer by R_HP · Sep 17, 2018 at 05:24 AM
@tormentoarmagedoom I added second line of code to the first one like this
if (creditCount >= costValue[turretIndex]) { creditCount -= costValue[turretIndex]; Instantiate(turretPrefabs[turretIndex], tile.transform.position, Quaternion.identity); print(turretPrefabs[turretIndex]); print(turretPrefabs[turretIndex].GetComponent<SellItem>().tile); tile.SetActive(false); turretPrefabs[turretIndex].GetComponent<SellItem>().tile = tile; canBuild = false; aud.PlayOneShot(turretDrop); }
there is another scripts "SellItem" which holds a variable "tile" . and i want to assign a game object to it
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a 3d text object represent the "health" of an object? 1 Answer