Hello guys, I want to do one simple thing in my game: the player could do sth after the he/she has clicked the right mouse button for 5 seconds. I wrote this in C#, but I failed. Then I wrote the same thing in js, it worked. I don’t know why, so I post the topic here for help, could anybody give me the reasons for my failing in C#?
// C# Code
using UnityEngine;
using System.Collections;
public class CSharp : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
print("CS:Left mouse button down!");
print("CS:SomeFunc()");
SomeFunc();
print("CS:SomeFunc() End");
}
}
IEnumerator SomeFunc()
{
print("CS:Wait for 5s...");
yield return new WaitForSeconds(5);
print("CS:smile:o sth after 5s...");
}
}
// Js code to do the same thing
// Update is called once per frame
function Update()
{
if (Input.GetMouseButtonDown(1))
{
print("JS:Left mouse button down!");
print("JS:SomeFunc()");
SomeFunc();
print("JS:SomeFunc() End");
}
}
function SomeFunc()
{
print("JS:Wait for 5s...");
yield WaitForSeconds(5);
print("JS:smile:o sth after 5s...");
}
Here are the results:
JS:Left mouse button down!
JS:SomeFunc()
JS:Wait for 5s…
JS:SomeFunc() End
CS:Left mouse button down!
CS:SomeFunc()
CS:SomeFunc() End
JS:smile:o sth after 5s…