- Home /
Is it possible to access a script in one duplicated game objets.
I have 8 towers in my game all with a script attached called "Tower", I want to access a variable in the script "Towers" in only ONE of the eight towers (selected by the player) from "GameManager" script, I tried creating a Game Object in the script "Towers" to store the selected tower in it (because the selection function exists in "Towers") then access it (the selected tower) and access the "Towers" script it contains, but it dosen't seem to work, any ideas?
Well, that should certainly be possible. What have you tried that didn't work?
I creating a variable "Selected" (GameObject) in the "Towers" script, so when the player selects a tower, it affects to the variable, then tried to access the variable from the "Game$$anonymous$$anager" and access the "Towers" script in the selected tower. a fast demonstartion (not real code just sort of algorithm) 1.Class Towers { public Game Object Selected; void On$$anonymous$$ouseDown() { //the player selects the tower Selected = gameObject;} 2.Game$$anonymous$$anager { //trying to access the towers script //trying to access the tower affected to "selected" //Access the "Towers" script in the selected tower ...} Here is the problem, I can't access it with Find because I don't know which tower will be selected, neither i can use static for the "selected" variable because it only allows to change its value.
I thought about giving a unique tag when the player selects a tower, but the problem is that I am already using tags to define if the towers is for the red player or blue one.
We need to see some code, especially that which attempts what you describe but doesn't seem to work, and how the entry selection is 'remembered' when selected. Without that we are probably wasting time and effort.
The program$$anonymous$$g concept you're discussing is called a class. The class is what's named "Towers". The script is just a text file.
It's not clear what do you mean by "access"? When do you want to access it (when user has selected a tower?). In any case I'd suggest you to go with ScriptableObjects to store current selected tower, thus you can maintain your architecture loosely coupled. There is also a way to handle events with scriptable objects, here is an example: https://github.com/kolodi/SceneAgnostic
Answer by berkanmusellim · Aug 27, 2018 at 12:47 PM
Hello,
Did you try to acces uniq ID of the GameObject, which is your selected tower?
You can learn more about the uniq ID in the link below.
https://docs.unity3d.com/ScriptReference/Object.GetInstanceID.html
THAN$$anonymous$$ YOU! I didn't know this thing exists. You are the best.
Answer by II_Spoon_II · Aug 27, 2018 at 11:42 AM
using UnityEngine;
public class Towers : MonoBehaviour {
public int NrOfUnits ;
public GameObject BaseTower; //the var that the GameManager will access
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void OnMouseDown()
{
//accessing the game manager
GameObject theGameManager = GameObject.Find("GameManager");
TheGameManager SelectionScript = theGameManager.GetComponent<TheGameManager>();
//if no tower is selected, the selected one will be the base tower
if (SelectionScript.isTarget == false)
{
SelectionScript.selectedBaseTower = gameObject;
SelectionScript.Position = this.gameObject.transform.position;
SelectionScript.isTarget = true;
//affecting the selected tower to the var
BaseTower = gameObject;
}
//if a tower is already selected, the second selection will be the target tower
else
{
if (this.gameObject != SelectionScript.selectedBaseTower)
{
SelectionScript.selectedTargetTower = gameObject;
SelectionScript.Move = true;
}
else
{
SelectionScript.selectedBaseTower = null;
SelectionScript.selectedTargetTower = null;
SelectionScript.isTarget = false;
SelectionScript.Move = false;
BaseTower = null;
}
}
}
}
and this is the GameManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TheGameManager : MonoBehaviour
{
public GameObject selectedBaseTower;
public GameObject selectedTargetTower;
public bool isTarget = false;
public Vector3 Position;
public GameObject[] units;
public GameObject[] unitsInst;
public Vector3 spawnValues;
public float spawnWait;
public float spawnMostWait;
public float spawnLeastWait;
public int startWait;
public bool stop;
int randEnemy;
public bool Move = false;
// Use this for initialization
void Start()
{
StartCoroutine(waitSpawner());
unitsInst = new GameObject[units.Length];
for (int i =0; i < units.Length; i++)
{
unitsInst[i] = Instantiate(units[i]);
unitsInst[i].SetActive(false);
unitsInst[i].transform.Translate(Position.x,Position.y,Position.z);
}
}
// Update is called once per frame
void Update()
{
//spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
if(Move == true)
{
for (int i = 0; i < unitsInst.Length; i++)
{
unitsInst[i].SetActive(true);
}
}
//selectedBaseTower = null;
//selectedTargetTower = null;
}
IEnumerator waitSpawner()
{
if (Move==true)
randEnemy = Random.Range(0, unitsInst.Length);
//Vector3 spawnPosition = new Vector3(selectedBaseTower.transform.position.x, selectedBaseTower.transform.position.y, selectedBaseTower.transform.position.z);
Instantiate(unitsInst[randEnemy], Position , unitsInst[randEnemy].transform.rotation);
// accessing selected tower
//I know thiswon't work, static didn't either and I couldn't figure it out
GameObject temp = Towers.BaseTower;
Towers TempTower = temp.GetComponent<Towers>();
TempTower.BaseTower..
yield return new WaitForSeconds(1);
}
}
Now to explain more, what I mainly want to do, is when a player selects a base and target tower, the gameManager reduces the NrOfUnits in the base tower. @JVene
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using a variable value with GetComponent 1 Answer
How Do I Access and Change Items in a List on Another Script? 2 Answers