- Home /
The question is answered, right answer was accepted
how can i access to the scripts which have same name and were attached to the same gameobject.
Hello: i'm following the official tutorial name Survival Shooter on website. I created a empty game object, use it as a enemy spawn point controller. i attached a script to it three times
and this is my script:
public class EnemyManager : MonoBehaviour { public playerHealth playerHealth; public GameObject enemy; public float spawnTime = 3f; public Transform[] spawnPoints;
void Start()
{
InvokeRepeating("Spawn", spawnTime, spawnTime);
}
void Spawn()
{
if (playerHealth.currentHealth<=0)
{
return;
}
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
the three script components have the same name but have different enemy prefabs references, i used them control the spawn time. now , i want to access those scripts components so that i can modify the spawn time from another script in a start menu(in another scene). i can change those spawn time for purpose of changing game difficulty. can anyone please help me to solve this? thanks?
=======================================
i used the method provided by @tanoshimi : The mistake you're making is, having correctly grabbed each script separately with GetComponents, you're then choosing an arbitrary one again with an unnecessary call to GetComponent. Try this instead:
EnemyManager[] scriptcompts = this.GetComponents(); scriptcompt_bunny = scriptcompts[0]; scriptcompt_bear = scriptcompts[1]; scriptcompt_Hellephant = scriptcompts[2];
scriptcompt_bear.spawnTime = 100; scriptcompt_bunny.spawnTime = 200; scriptcompt_Hellephant.spawnTime = 300;
it works just the way i want it.
thanks for the answers from you guys.
Hello,
I think you should create 3 different gameObjects, put each script in one of those gameObjects.
If needed then create another script that will get reference to each of those Enemy scripts and calculate the spawn time or whatever. Hope it works
thanks. it is an easy and effective way, i just wonder if i can change the value of spawntime in inspector, maybe i can modify them through script. and i can have another option,
Answer by Geometrical · Mar 06, 2017 at 01:43 AM
Simple:
EnemyManager[] components = this.GetComponents<EnemyManager> ();
EnemyManager firstComponent = components [0];
EnemyManager secondComponent = components [1];
EnemyManager thirdComponent = components [2];
I tried this method, but the outcome is that only the thirdComponent is assigned to components[0], my script is:
Enemy$$anonymous$$anager[] scriptcompts = this.GetComponents<Enemy$$anonymous$$anager>();
scriptcompt_bunny = scriptcompts[0];
scriptcompt_bear = scriptcompts[1];
scriptcompt_Hellephant = scriptcompts[2];
scriptcompt_bear.GetComponent<Enemy$$anonymous$$anager>().spawnTime = 100;
scriptcompt_bunny.GetComponent<Enemy$$anonymous$$anager>().spawnTime = 200;
scriptcompt_Hellephant.GetComponent<Enemy$$anonymous$$anager>().spawnTime = 300;
The mistake you're making is, having correctly grabbed each script separately with GetComponents, you're then choosing an arbitrary one again with an unnecessary call to GetComponent. Try this ins$$anonymous$$d:
Enemy$$anonymous$$anager[] scriptcompts = this.GetComponents<Enemy$$anonymous$$anager>();
scriptcompt_bunny = scriptcompts[0];
scriptcompt_bear = scriptcompts[1];
scriptcompt_Hellephant = scriptcompts[2];
scriptcompt_bear.spawnTime = 100;
scriptcompt_bunny.spawnTime = 200;
scriptcompt_Hellephant.spawnTime = 300;
Thanks! it works very well! just the way i want it!
Answer by AMU4u · Mar 06, 2017 at 01:38 AM
I've never seen anyone do that before! It works? Interesting stuff.
Create a new GameObject that we will use as a spawn controller. Name it as such. now create an array inside the controller class for the spawns points/class you create. Since it is a controller, let's make it a singleton, a script/class that has global variables that cannot be cloned or duplicated during a build.
public class SpawnController : Monobehavior{
public static SpawnController instance = null;
[System.Serializable] //EnemyManager class must be serialized, as well.
public EnemyManager[] enemyManagerArray;
void Start()
{
if(instance == null)
{
this = instance;
}
else if(instance != this) //if instance IS NOT equal to this object in memory
{
Destroy(gameObject); //destroy this GameObject, there cannot be two!!
}
}
Attach this script to the GameObject SpawnController.
Now in your EnemyManager script, tell it to add itself to the array in the SpawnManager we built.
public class EnemyManager : MonoBehaviour {
public playerHealth;
playerHealth;
public GameObject enemy;
public float spawnTime = 3f;
public Transform[] spawnPoints;
void Start()
{
SpawnManager.instance.enemyManagerArray.Add(this);//add it!!
InvokeRepeating("Spawn", spawnTime, spawnTime);
}
void Spawn()
{
if(playerHealth.currentHealth<=0)
{
return;
}
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
Now when the scripts start, they will add themselves to the array in the manager singleton we created!
Now, to change something in the array, we put some relevant functions in some code in a relevant controller.
void RandomlyChooseEnemyScriptThenPassToOtherFunction()
{
SpawnManager randomScriptToChoose = UnityEngine.Random.Range(0, SpawnManager.instance.enemyScriptArray.Length)];
ChangeEnemyArrayValue(randomScriptToChoose);
}
void ChangeEnemyArrayValue(EnemyManager enemyScript)
{
enemyScript.SpawnTime = UnityEngine.Random.Range(0,1) ;
}
Thanks. but when i follow your method, i have some questions:
1. "[System.Serializable]//Enemy$$anonymous$$anager class must be serialized, as well. public Enemy$$anonymous$$anager[] enemy$$anonymous$$anagerArray;" I found that System.Serializable is not necessary to "public ", or i will get a compile error. and i also found that if i used array here, then i can not use the Add function in another script(the enemymanager script), so i use list ins$$anonymous$$d. my code at this step is : public List enemy$$anonymous$$anagerArray;
about this line: "Spawn$$anonymous$$anager randomScriptToChoose = UnityEngine.Random.Range(0, Spawn$$anonymous$$anager.instance.enemyScriptArray.Length)];" i got 2 errors here: first, the Random.Range function returns a int type, should i change the type of randomScriptToChoose from Spawn$$anonymous$$anager to int? and at the end of the line, there is "]" symbol, i also got a error from there, anything wrong? i'm trying to fix them myself, the result is : int randomScriptToChoose = UnityEngine.Random.Range(0, SpawnController.Instant.enemy$$anonymous$$anagerArray.Count);
about the ChangeEnemyArrayValue(Enemy$$anonymous$$anager enemyScript) function, i was asked to pass a Enemy$$anonymous$$anager type parameter into it, but if i try to pass randomScriptToChoose (no matter is int type or Spawn$$anonymous$$anager)into it , i will get error.
i will appreciate if you can help me with this.
Follow this Question
Related Questions
How to add a Script to a GameObject during Runtime [2016] 3 Answers
Accessing Variables from another object Script 1 Answer
How to detect if two objects become a square ? 1 Answer
Please help my head is burning from this problem : i have multiple gameobject , same script 1 Answer
Script to Keep One GameObject from Being Destroyed 0 Answers