making object on/off with coroutine.

Hello guys,

I am using following code to turn on/off my object OnMouseButton down and Inactive when i do mouse up.
but unable to achive desired result. Please Help.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class RingControl : MonoBehaviour {
boolflg;

void OnMouseDown ()
 {
flg =true;
 }
void OnMouseUP()
 {
flg=false;
 }
public List<GameObject> rings;

voidUpdate(){
StartCoroutine(RControl ());
 }

IEnumeratorRControl(){

if (flg==false)
 {
foreach(var items in rings)

 {
items.SetActive(false);
 }
 }
if (flg==true)
{
foreach(var items in rings)
 {
items.SetActive(true);
yield return new WaitForSeconds(2);
items.SetActive(false);
 }
 }
 }

You are starting a new coroutine each frame (Update).
Do you really need a coroutine?

void OnMouseDown ()
{
    UpdateRings(true);
}

void OnMouseUP()
{
    UpdateRings(false);
}

public List<GameObject> rings;

void UpdateRings(bool visible)
{
    foreach(GameObject ring in rings)
        ring.SetActive(visible);
}

If this is not what you want, please give more details on what you are trying to do.

obs: Fix your code indentation, it’s very hard to read as is

Also, “voidUpdate()” is probably not what you wanted. I suppose it should have read “void Update()” (note the space.) But as L-Tyrosine pointed out, you’re probably on the wrong track anyway.

No i dont really want coroutine.My task is to make my rings On/off on mouse button pressed.And Rings should permanently disapeard if i do mouse up.The script which u write i only do active rings not makitng them on.off and not deactivating on mouse button up. Please reply

can u please tell me what i have to do to fix that.

I think you should try to solve the problem yourself, these kind people gave you the advice and told you what does not work with your script, where it is at fault. Asking other people to give you the solution is not the way it’s supposed to go. Perhaps you could give Unity tutorials a try, they have great examples. Even other Unity Tutorial projects in Asset Store provide nice examples of how to write scripts. Good luck!

2 Likes

Can you please give me some hints of any tatorial on asset stroe to make game like Cake maker or Doctor clinic?

http://www.microsoftvirtualacademy.com/training-courses/c-fundamentals-for-absolute-beginners

Go to “topics” and read each one of the tutorials, also read the whole manual. Take your time, spend weeks in this process if needed. There’s no way to code a complete game without solid knowledge on Unity and C#

Like L-Tyrosine said, give yourself some time to understand how Unity works, what are the best practices and how to code scripts. Even though these tutorials are not “Cake Maker” or “Doctor Clinic” type of games, understanding them will enable you to produce your own code for your game. Since I started visiting forums regularly I noticed you open a thread each time you get stuck with a script. Basically I think it is mandatory you give some time to acquire knowledge how to write code for Unity games.

If you need help on how to accomplish some complicated task or which design pattern to use, I’m sure most people will help you out. But asking them to give you the finished code is plain wrong. Good luck with tutorials!