Using Unity 5.2.2.1f.
following code sample:
yield WaitForSeconds (0.1);
Error:
Error 1 The type or namespace name ‘yield’ could not be found (are you missing a using directive or an assembly reference?)
But it was in the intellesense drop down.
Using Unity 5.2.2.1f.
following code sample:
yield WaitForSeconds (0.1);
Error:
Error 1 The type or namespace name ‘yield’ could not be found (are you missing a using directive or an assembly reference?)
But it was in the intellesense drop down.
What’s the context you’re using it in? IE - what is the code surrounding that statement.
Weird, just cut and pasted the example from the docs and changed the name, now it compiles. Hmmmm.
Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement
using UnityEngine;
using System.Collections;
public class WaitForSecondsExample : MonoBehaviour {
void Start() {
StartCoroutine(Example());
}
IEnumerator Example() {
print(Time.time);
yield return new WaitForSeconds(5);
print(Time.time);
}
}
That’s a different error, but whatever.
It’s throwing that error in that file? What line? Also - please wrap your code in code tags so it’s easier to read.
No errors, but not pausing the script execution. Thank you for your help.
I’m assuming that I’m not seeing the color change because the Update keeps firing and setting the color back even with the StartCoroutine?
// Update is called once per frame
void Update ()
{
// if lest mouse button is clicked
// create a raycast that originate from the mouse clicked position
if(Input.GetMouseButtonDown(0))
{
Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(rayOrigin, out hitInfo, distance))
{
Debug.Log("Your are casting a ray");
Debug.DrawRay(rayOrigin.direction, hitInfo.point, Color.green);
if (hitInfo.rigidbody != null)
{
Debug.Log("applying force");
AddHighlight(hitInfo.collider.gameObject);
hitInfo.rigidbody.AddForceAtPosition(rayOrigin.direction * power, hitInfo.point);
}
else
{
Debug.Log("null?");
}
}
}
}
IEnumerator WaitThreeSeconds()
{
print(Time.time);
yield return new WaitForSeconds(5);
print(Time.time);
}
void AddHighlight(GameObject gameObj)
{
Color color = gameObj.GetComponent<Renderer>().material.color;
color.r = color.r * 10;
color.g = color.g * 10;
color.b = color.b * 10;
gameObj.GetComponent<Renderer>().material.color = color;
StartCoroutine("WaitThreeSeconds");
color = gameObj.GetComponent<Renderer>().material.color;
color.r = color.r / 10;
color.g = color.g / 10;
color.b = color.b / 10;
gameObj.GetComponent<Renderer>().material.color = color;
}
Again - code tags: Using code tags properly - Unity Engine - Unity Discussions
It’s pretty hard to provide any guidance since the error you’re getting has been different every time you’ve posted and now you’re apparently not getting any errors at all. You also mentioned changing color in a coroutine but your coroutine doesn’t change the color of anything. StartCoroutine doesn’t actually make the execution pause which is why the code after it is executing immediately. If you want to wait before changing it back then AddHighlight has to be a coroutine.
OK, sorry about the missing tags, first time really posting here. Trying to work with code examples that have been changed by newer Unity3D API I’m thinking.
I appreciate your help. I guess setting the color back to normal would be called from within the “WaitThreeSeconds” coroutine?
really just trying to briefly highlight an object when the user passes over it.
Indeed, the code after StartCoroutine("WaitThreeSeconds");
executes instantly because it’s not inside the coroutine.
The other issue is that this will execute every frame your mouse is on the object so you’re spawning a bunch of coroutines. You’ll need a boolean or something to maintain some state about whether or not the coroutine is running.
And your fwd Vector3 could just be this:
var fwd = transform.forward;
to skip the TransformDirection call.
Wonderful, working now. I’ll add the check so I don’t call the same co-routine for the same object while the timeout is still executing.
Thank you everyone!
Scott