Figuring out which function to call on button click
So I am an utter noob, but I spent the last 4 hours looking for a solution myself, but alas, I can't find what I need.
I'm using a prefab called LiquidPhysics.
I have 2 scripts, and I need a button to toggle between types of liquid:
ParticleGenerator.cs
using UnityEngine;
using System.Collections;
/// <summary
/// Particle generator.
///
/// The particle generator simply spawns particles with custom values.
/// See the Dynamic particle script to know how each particle works..
///
/// Visit: www.codeartist.mx for more stuff. Thanks for checking out this example.
/// Credit: Rodrigo Fernandez Diaz
/// Contact: q_layer@hotmail.com
/// </summary>
public class ParticleGenerator : MonoBehaviour {
float SPAWN_INTERVAL=0.025f; // How much time until the next particle spawns
float lastSpawnTime=float.MinValue; //The last spawn time
public int PARTICLE_LIFETIME=3000000; //How much time will each particle live
public Vector3 particleForce; //Is there a initial force particles should have?
public DynamicParticle.STATES particlesState=DynamicParticle.STATES.WATER; // The state of the particles spawned
public Transform particlesParent; // Where will the spawned particles will be parented (To avoid covering the whole inspector with them)
void Start() { }
void Update() {
if( lastSpawnTime+SPAWN_INTERVAL<Time.time ){ // Is it time already for spawning a new particle?
GameObject newLiquidParticle=(GameObject)Instantiate(Resources.Load("LiquidPhysics/DynamicParticle")); //Spawn a particle
newLiquidParticle.GetComponent<Rigidbody2D>().AddForce( particleForce); //Add our custom force
DynamicParticle particleScript=newLiquidParticle.GetComponent<DynamicParticle>(); // Get the particle script
particleScript.SetLifeTime(PARTICLE_LIFETIME); //Set each particle lifetime
particleScript.SetState(particlesState); //Set the particle State
newLiquidParticle.transform.position=transform.position;// Relocate to the spawner position
newLiquidParticle.transform.parent=particlesParent;// Add the particle to the parent container
lastSpawnTime=Time.time; // Register the last spawnTime
}
}
}
AND
DynamicParticle.cs
using UnityEngine;
using System.Collections;
/// <summary>
/// Dynamic particle.
///
/// The dynamic particle is the backbone of the liquids effect. Its a circle with physics with 3 states, each state change its physic properties and its sprite color ( so the shader can separate wich particle is it to draw)
/// The particles scale down and die, and have a scale effect towards their velocity.
///
/// Visit: www.codeartist.mx for more stuff. Thanks for checking out this example.
/// Credit: Rodrigo Fernandez Diaz
/// Contact: q_layer@hotmail.com
/// </summary>
public class DynamicParticle : MonoBehaviour {
public enum STATES{WATER,GAS,LAVA,POO,NONE}; //The 4 states of the particle
STATES currentState=STATES.NONE; //Defines the currentstate of the particle, default is water
public GameObject currentImage; //The image is for the metaball shader for the effect, it is onle seen by the liquids camera.
public GameObject[] particleImages; //We need multiple particle images to reduce drawcalls
float GAS_FLOATABILITY=7.0f; //How fast does the gas goes up?
float particleLifeTime=3.0f,startTime;//How much time before the particle scalesdown and dies
void Awake(){
if (currentState == STATES.NONE)
SetState (STATES.WATER);
}
//The definitios to each state
public void SetState(STATES newState){
if(newState!=currentState){ //Only change to a different state
switch(newState){
case STATES.WATER:
GetComponent<Rigidbody2D>().gravityScale=1.0f; // To simulate Water density
break;
case STATES.GAS:
particleLifeTime=particleLifeTime/2.0f; // Gas lives the time the other particles
GetComponent<Rigidbody2D>().gravityScale=0.0f;// To simulate Gas density
gameObject.layer=LayerMask.NameToLayer("Gas");// To have a different collision layer than the other particles (so gas doesnt rises up the lava but still collides with the wolrd)
break;
case STATES.LAVA:
GetComponent<Rigidbody2D>().gravityScale=0.3f; // To simulate the lava density
break;
case STATES.POO:
GetComponent<Rigidbody2D>().gravityScale=4.1f; // To simulate the lava density
break;
case STATES.NONE:
Destroy(gameObject);
break;
}
if(newState!=STATES.NONE){
currentState=newState;
startTime=Time.time;//Reset the life of the particle on a state change
GetComponent<Rigidbody2D>().velocity=new Vector2(); // Reset the particle velocity
currentImage.SetActive(false);
currentImage=particleImages[(int)currentState];
currentImage.SetActive(true);
}
}
}
void Update () {
switch(currentState){
case STATES.WATER: //Water and lava got the same behaviour
MovementAnimation();
ScaleDown();
break;
case STATES.LAVA:
MovementAnimation();
ScaleDown();
break;
case STATES.POO:
MovementAnimation();
ScaleDown();
break;
case STATES.GAS:
if(GetComponent<Rigidbody2D>().velocity.y<50){ //Limits the speed in Y to avoid reaching mach 7 in speed
GetComponent<Rigidbody2D>().AddForce (new Vector2(0,GAS_FLOATABILITY)); // Gas always goes upwards
}
ScaleDown();
break;
}
}
// This scales the particle image acording to its velocity, so it looks like its deformable... but its not ;)
void MovementAnimation(){
Vector3 movementScale=new Vector3(1.0f,1.0f,1.0f);//Tamaño de textura no de metaball
movementScale.x+=Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x)/30.0f;
movementScale.z+=Mathf.Abs(GetComponent<Rigidbody2D>().velocity.y)/30.0f;
movementScale.y=1.0f;
currentImage.gameObject.transform.localScale=movementScale;
}
// The effect for the particle to seem to fade away
void ScaleDown(){
float scaleValue = 1.0f-((Time.time-startTime)/particleLifeTime);
Vector2 particleScale=Vector2.one;
if (scaleValue <= 0) {
Destroy (gameObject);
} else{
particleScale.x=scaleValue;
particleScale.y=scaleValue;
transform.localScale=particleScale;
}
}
// To change particles lifetime externally (like the particle generator)
public void SetLifeTime(float time){
particleLifeTime=time;
}
// Here we handle the collision events with another particles, in this example water+lava= water-> gas
void OnCollisionEnter2D(Collision2D other){
if(currentState==STATES.WATER && other.gameObject.tag=="DynamicParticle"){
if(other.collider.GetComponent<DynamicParticle>().currentState==STATES.LAVA){
SetState(STATES.GAS);
}
}
}
}
And I have no Idea how and which function I should add where in my button component. Should I write my own function that forwards a specific script to set the particle state to DynamicParticle.STATES.LAVA for example?
There is just too much going on for my brain to understand at this time...
Any help is greatly appreciated!
Your answer
![](https://koobas.hobune.stream/wayback/20220612071329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i change a state when a button is pressed? 1 Answer
Dragging a C# script to onClick button returns no listed functions 0 Answers
UI buttons take too much to time to unpress on mobile 0 Answers
Im trying to make gameObject move only if specific button is pressed 1 Answer
How to wait for a button response 0 Answers