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.

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) ;
}

Simple:

EnemyManager[] components = this.GetComponents<EnemyManager> ();
EnemyManager firstComponent = components [0];
EnemyManager secondComponent = components [1];
EnemyManager thirdComponent = components [2];