How to stop an emission from another object

Hi, I have this scene where if the player is in a collision zone and they press a button then the emission of water should stop, wait 3 seconds then turn back on again. If the player is in the water zone whilst the emission is on then they get reset. I think I have most of the code done, just struggling to find how to turn off the water. Thanks

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class waterTrigger : MonoBehaviour
{
    public bool WaterOn = true;
    

    void start()
    {
        StartCoroutine(ChangeWater());
    }

    void OnCollisionEnter(Collision gameObjectInformation) //if player is in area and x is pressed turn water off
    {
        if (gameObjectInformation.gameObject.name == "player")
        {
           

            if (Input.GetKeyDown(KeyCode.Joystick1Button2))
            {
                WaterOn = false;
            }
        }
    }

    public IEnumerator ChangeWater()
    {
        if (WaterOn == true)
        {
            yield return new WaitForSeconds(3f);// time it takes for water to turn back on
            WaterOn = false;
        }
    }
}

and WaterfallCol:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaterfallCol : MonoBehaviour
{
    private waterTrigger WaterTrigger;
    public ParticleSystem water;

    // Start is called before the first frame update
    void Start()
    {
        WaterTrigger = GameObject.Find("InteractZone").GetComponent<waterTrigger>();
    }


    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "player" && WaterTrigger.WaterOn == true) // If player is in water zone and water is on reset player
        {
            water.Emit;
            collision.transform.position = new Vector3(0f, 2.5f, 0f);
        }
    }
}

Assuming I understand right… try this.

In your waterTrigger script:

  1. If your trigger zone is a “trigger” you need to use OnTrigger, not OnCollision. Try OnTriggerStay.
  2. Move your StartCoroutine out of Start and put it in your buttondown if statement.
  3. Swap the states of WaterOn in your coroutine. "if(WaterOn == false) … yield … WaterOn = true;

In your WaterfallCol script:

  1. Again, if this is a trigger, you should use OnTrigger.
  2. Might need to add an else statement that stops the water emission if WaterOn is false.

A few things jump out at me…

void start()
{
     StartCoroutine(ChangeWater());
}

Is the above supposed to be a (capitalised) MonoBehaviour Start function? (if yes then capitalise it, no then I’d recommend renaming it, you’ll only confuse things giving it that name).

water.Emit;

What’s the above supposed to do? This isn’t a function call and WaterTrigger has no field or property with that name. Did you mean to call water.start() there? (if so then see point 1 and rename start to something like TurnWaterOffTemporarily because it looks that’s what it’s supposed to do)

You never set WaterOn to true except on initialisation. So (if you were calling the function properly) the collision turns the water off but it never gets turned on again. I suspect that ChangeWater should look like this…

public IEnumerator ChangeWater()
{
    if (WaterOn)
    {
         WaterOn = false;
         yield return new WaitForSeconds(3f);// time it takes for water to turn back on
         WaterOn = true;
     }
}

Finally, add some Debug.Log lines so you know what functions are being called and when. Always a good idea when dealing with collisions so that you know whether the problems are related to the code or the set-up (ie are the collision callbacks doing the wrong thing, or just not getting called?) When you’ve got it working, comment the logging lines out so it’s easy to reinstate them if it goes wrong again.