- Home /
Turn weather on and off
I have been working on the following script to allow me to click on a grid in my game and check the tag of it, If it has the tag world blocks it does something to only that block. What I am trying to do now it make it so when you click on a block it turns on or off the particlesystem that is childed to it. I have the particle childed to the block. I am not sure how to make it so when you click it checks if it is on or off and and switches the state of it.
This is my updated script I have so far it is getting close to what I need just cant get all fixed.
Here is the script:
using UnityEngine;
using System.Collections;
public class ChangeWeather : MonoBehaviour
{
public bool active = true;
private Transform child;
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == "WorldBlocks")
{
if(active == true)
{
foreach(Transform child in hit.transform)
{
child.Active = false;//This part is not working.
}
active = false;
}
if(active == false)
{
foreach(Transform child in hit.transform)
{
// child.Active = false; This part is not working
}
active = true;
}
}
}
}
}
}
So what I am wanting to do is get the child of the clicked game object and turn it off or on. I have named the gameObject weather.
Do you have the particle system attached to the game object that is being clicked or on a child game object?
The child game object is a Shrunken particle system. So its the child I am wanting turned off and on.
well to make it simple, you could create a variable for the particle emitter and simple disable it or enable it when button is clicked
All I need is to find the child of clicked gameobject and disable its child.
Answer by davidflynn2 · Jul 15, 2013 at 08:52 PM
I ended up having to redo a lot of different things but this is what I got to work:
if(hit.collider.tag == "WorldBlocks")
{
if(active == true)
{
foreach(Transform child in hit.transform)
{
child.renderer.enabled = true;
}
active = false;
}
}
Answer by robertbu · Jul 15, 2013 at 07:03 PM
A Particle System is not a game object...it is a Component. So if you attached the component to the same game object that is being clicked you can do:
ParticleSystem ps = hit.collider.gameObject.particleSystem;
if (ps.isPlaying)
ps.Stop();
else
ps.Play();
If I add the particle system to my current game object is there any way of moving the particle system up so it falls down onto the object I am trying to make it snow.
so you have the particle system attached to an empty game object and that object is a child of the block you're clicking? If so, you can still use Robert's answer. just do child.particleSystem.Stop();
If the Particle Systems is attached to a child, you need to first gain access to that child. You can use drag and drop onto a variable at edit time, or you could use Transform.Find(). If your child was named 'Snow', it would be:
GameObject goChild = hit.transform.Find("Snow");
ParticleSystem ps = goChild.particleSystem;
if (ps.isPlaying)
ps.Stop();
else
ps.Play();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Particle memory overhead 1 Answer
Can't get a coroutine to work as expected. 2 Answers
Getting XP when dies Help 1 Answer