- Home /
Game Object not activating when commanded by function in another script.
Hi, this is my first time asking anything in here and really, my first time doing anything semi-serious in Unity.
I ran into a bit of a problem that has me stuck:
I'm making a game that takes the Roll a Ball tutorial's basis and expands on it as a practice of what I can do, so most of the code is mostly a copy of the tutorial.
The think that has kept me stuck is that I have a particle system attached to an Empty Game Object as a child. The following code is supposed to deactivate the Empty Game Object (plus the particle sys) and reenable it after the itemCount (from the script called PlayerController) reaches 4.
public class LevelExit_Portal_Controller : MonoBehaviour {
public GameObject portal;
public GameObject player;
void Start ()
{
portal.gameObject.SetActive (false);
}
void Update ()
{
PlayerController playerController = player.GetComponent<PlayerController> ();
if (playerController.itemCount == 4) {
OnPortalActivation ();
}
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Player")) {
Application.Quit ();
}
}
void OnPortalActivation ()
{
portal.SetActive (true);
}
}
All code is valid according to the Editor and MonoDevelop correctly detects the script reference. The code snippet that activates the win message that contains itemCount also runs fine. The problem is that the Empty Game Object is not activating, therefore not allowing it to be used to change level with collision detection and neither it activates the Particle System child.
What am I doing wrong? Am I missing something? Should I put all this code on the same script as the Player Game Object (as in, put it in PlayerController instead of its own script?)
What object did you put this script on? Once the gameobject is turned off by SetActive(false), all its script stop functioning. $$anonymous$$aybe you can try put this script on the parent empty object, but set the portal field to be the child (particle system) itself.
The script is on the Parent Empty Game Object the ParticleSys is attached to as seen here:
Are you sure that the player has IsTrigger checked in the inspector? And are you sure your player's item count is actually reaching 4?
buzz is right...you're object is levelexit_Portal and the portal is defined as that too, so you are deactivating the gameobject that has the script to reactivate it.
Put it all in a script on your player object, or a gamemanager empty object.