Activate a script if an object is in the game world?

So i have an enemy spawning script, and i only want it to activate if another object is in the game world/when i place the object in the world. i have no clue how to express that i am so new to scripting.
can someone help me add that line of code to the script please?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class EnemySpawner : MonoBehaviour
{

	public GameObject[] Objectman;
	public float TimeSpawn = 3;
	public int MaxObject = 10;
	public string PlayerTag = "Player";
	public bool PlayerEnter = true;
	private float timetemp = 0;
	private int indexSpawn;
	private List<GameObject> spawnList = new List<GameObject> ();
	public bool OnActive;
	
	void Start ()
	{
		indexSpawn = Random.Range (0, Objectman.Length);
		timetemp = Time.time;
	}

	void Update ()
	{
		OnActive = false;
		if (PlayerEnter) {
			// check if player is enter this area
			GameObject[] playersaround = GameObject.FindGameObjectsWithTag (PlayerTag);
			for (int p=0; p<playersaround.Length; p++) {
				if (Vector3.Distance (this.transform.position, playersaround [p].transform.position) < this.transform.localScale.x) {
					OnActive = true;
				}
			}
		} else {
			OnActive = true;
		}
		
		bool offlineMode = (!Network.isServer && !Network.isClient);

		if (!OnActive)
			return;
		
		ObjectExistCheck ();
		if (Objectman [indexSpawn] == null)
			return;
		
		// spawn if ObjectsNumber is less than Max object.
		if (ObjectsNumber < MaxObject && Time.time > timetemp + TimeSpawn) {
			timetemp = Time.time;
			GameObject obj = null;
			Vector3 spawnPoint = DetectGround (transform.position + new Vector3 (Random.Range (-(int)(this.transform.localScale.x / 2.0f), (int)(this.transform.localScale.x / 2.0f)),0, Random.Range ((int)(-this.transform.localScale.z / 2.0f), (int)(this.transform.localScale.z / 2.0f))));
			if (!offlineMode) {
				if (Network.isServer){
					obj = (GameObject)Network.Instantiate (Objectman [indexSpawn], spawnPoint, Quaternion.identity, 0);
				}
			} else {
				obj = (GameObject)GameObject.Instantiate (Objectman [indexSpawn], spawnPoint, Quaternion.identity);
			}
			if (obj)
				spawnList.Add (obj);
			indexSpawn = Random.Range (0, Objectman.Length);
			
		}
	}
	
	private int ObjectsNumber;

	void ObjectExistCheck ()
	{
		// checking a number of all objects. that's was spawn with this spawner
		ObjectsNumber = 0;
		foreach (var obj in spawnList) {
			if (obj != null)
				ObjectsNumber++;
		}
	}
	
	void OnDrawGizmos ()
	{
		#if UNITY_EDITOR
		Gizmos.color = Color.red;
		Gizmos.DrawSphere (transform.position, 2);
		Gizmos.DrawWireCube (transform.position, this.transform.localScale);
		Handles.Label(transform.position, "Enemy Spawner");
		#endif
	}
	
	Vector3 DetectGround (Vector3 position)
	{
		RaycastHit hit;
		if (Physics.Raycast (position, -Vector3.up, out hit, 1000.0f)) {
			return hit.point;
		}
		return position;
	}
	
}

On Line 60 :
obj = (GameObject)GameObject.Instantiate (Objectman [indexSpawn], spawnPoint, Quaternion.identity);
just add a line :

 obj.AddComponent<WhatScriptYouWantToAttach>();

it will add the script as it instantiate to what ever place
Happy Codding :slight_smile:

To check if object is in active state use this

if(Player.activeInHierarchy)

then activate the game object for which the enemy spawning script is attached by simple using this

EnemySpawningObj.SetActive(true);

Hope this may help you.
Nsks

sorry everyone, this is kinda hard for me to explain. the script i posted above, i only want it to be activated when another object is in the scene. a cube, anything, so i would have to have a public variable for that object right? so the script knows what object to look for?

the entire script i posted above. after i add “if(GameObject.Find(“Cube”))”
then i want to activate the script itself. i only want the script to run if it find the predetermined object.

I’m really sorry I cant understand what you’re saying.

If you want to see if an object is in the scene use

GameObject cube = Gameobject.Find("NameOfTheCube");

Once you have found that game object get the component ‘Script’ from it with:

CubeScriptYouWantToActivate cs = cube.GetComponent<CubeScriptYouWantToActivate>();

CubeScriptYouWantToActivate.SetActive(true);
CubeScriptYouWantToActivate.CallAnyMethod();
CubeScriptYouWantToActivate.CallAnotherMethod(true);
CubeScriptYouWantToActivate.SetFloatInsideScript(11);

Then this is the script on the object:

CubeScriptYouWantToActivate.cs

public void CallAnyMethod()
{
    this.GameObject.SetActive(true);
}
public void SetFloatInsideScript(float number)
{
    Debug.Log(number);
}

“i would need to check for the gameobject before or in the void start/void update parts right? and if gameobject.find is true then the rest will start right?”

Correct, you should have a manager in the scene that checks to see when things are edited or not. It’s not good practice to have one script rely on another without having a manager because 1. you cant debug it well 2. if you come back later or someone else works on the project they have no idea what or how things are being activated.