how to enable (disabled) spawner?

Hi

i have a game , if the user reached specific number of score for example if player reached 100 score , an exist( disabled spawner from inspector ) will be enabled , would you please tell me how to do so ? :face_with_spiral_eyes:

There’s a different between a MonoBehaviour being enabled, and a GameObject being active.

The first step is getting a reference to the spawner. Then either activating it’s gameObject or enabling the MonoBehaviour.

Without seeing your relevant code, here’s an example I made up.

using UnityEngine;

public class User : MonoBehaviour {
  // Assign in inspector
  public Spawner m_spawner;

  public AwardScore(int points) {
    score += points;
    if (score >= 100) {
      m_spawner.gameObject.SetActive(true);
      m_spawner.enabled = true;
    }
  }
 
  int score { get; set; }
}

i have multible of spawners which each one has its own class with its own behavior and codes, so how to activate one of them ??

You have to find the spawner you want.

I suggest making a SpawnerManager to track each spawner and let you access the one you want. If you have something initializing the scene, it can pass the SpawnerManager to whatever script needs it. You can also make it a singleton.

Otherwise, here is a way to do it using a Find command, just don’t use any of the Find commands every frame. This command only works on ACTIVE game objects, so find the objects before they’re deactivated if they are getting deactivated. I assume it’ll find disabled Monobehaviours, though I’m not 100% sure on that.

Spawner[] allSpawners;

Spawner FindSpawner() {
  // Only finds ACTIVE gameobjects.
  allSpawners = Object.FindObjectsOfType(typeof(Spawner)) as Spawner[];
}

void ActivateSpawner() {
  foreach (Spawner s in allActiveSpawners) {
    // psuedo code. You'll have to figure out which one you want.
    if (s is the spawner we want) {
      s.enabled = true;
    }
  }
}

Just buy Core GameKit. It’s on sale right now for 96% off!

http://forum.unity3d.com/threads/246387-DarkTonic-insanity-Sale!-5-Master-Audio-2-Core-GameKit!-92-96-off!