problem with c# and Yield

Hello, as I have a problem, INSTRUCTIONAL occupy the “Yield” in C# in a “While” marks the following error:

“…cs(108,10): error CS1624: The body of ....DragObject(float, UnityEngine.Vector3, UnityEngine.Vector3)' cannot be an iterator block because void’ is not an iterator interface type”"

in:

while (Input.GetMouseButton (0))
{
Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition)
ray.GetPoint springJoint.transform.position = (distance);

yield return null; //HERE
}

help me?, thanks.

You didn’t show the definition of DragObject… I’m assuming the code you listed is within a method named DragObject, declared as void DragObject(float f, Vector3 v1, Vector3 v2) { … }{/i]. If that’s the case, just replace ‘void’ with ‘IEnumerator’ (and add using System.Collections; if you don’t already have it).
If that doesn’t help, post a more complete code sample.

oh! yeah!, thanks very much!, i will test.!, and yes! i was use the Script “DragObject”

hi, i have problems why i am trying to convert from Java to C# this code:

Java

and in C# i have this:

but, in C# not work very well

Why?..

Help me please!!
thanks!.

and if anything, I’m omitting the part of giving birth to objects.

the name of script is “DragObjects” .

Ok,solved the problem, I solved it myself, here the Solution:

using UnityEngine;
using System.Collections;

public class dos : MonoBehaviour 
{

    #region Variables

    public float spring = 50.0f;
    public float damper = 5.0f;
    public float drag = 10.0f;
    public float angularDrag = 5.0f;
    public float distance = 0.2f;  
    public float pushForce = 0.2f;
    public bool attachToCenterOfMass = false;
    public Material highlightMaterial;

    private GameObject highlightObject;
    private SpringJoint springJoint;

    #endregion

    void Update()
    {
        Camera mainCamera = FindCamera();

        highlightObject = null;

        // We need to actually hit an object
        RaycastHit hit_01;
        if(Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit_01, 100.0f))
        {
            if(hit_01.rigidbody  !hit_01.rigidbody.isKinematic)
            {
                highlightObject = hit_01.rigidbody.gameObject;
            }
        }
		
		

        // Make sure the user pressed the mouse down
        if(!Input.GetMouseButtonDown(0))
            return;


        // We need to actually hit an object
        RaycastHit hit;
        if(!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 100.0f))
        {
            return;
        }
        // We need to hit a rigidbody that is not kinematic
        if(!hit.rigidbody || hit.rigidbody.isKinematic)
        {
            return;
        }

        if(!springJoint)
        {
            GameObject go = new GameObject("Rigidbody dragger");
            go.AddComponent("Rigidbody");
            springJoint = (SpringJoint)go.AddComponent("SpringJoint");
            go.rigidbody.isKinematic = true;
        }

        springJoint.transform.position = hit.point;
        if(attachToCenterOfMass)
        {
            Vector3 anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
            anchor = springJoint.transform.InverseTransformPoint(anchor);
            springJoint.anchor = anchor;
        }
        else
        {
            springJoint.anchor = Vector3.zero;
        }

        springJoint.spring = spring;
        springJoint.damper = damper;
        springJoint.maxDistance = distance;
        springJoint.connectedBody = hit.rigidbody;

		
        StartCoroutine(DragObject(hit.distance, hit.point, mainCamera.ScreenPointToRay(Input.mousePosition).direction));
    }

    IEnumerator DragObject(float distance, Vector3 hitpoint, Vector3 dir)
    {
		
        float startTime = Time.time;
        Vector3 mousePos = Input.mousePosition;


        float oldDrag = springJoint.connectedBody.drag;
        float oldAngularDrag = springJoint.connectedBody.angularDrag;
        springJoint.connectedBody.drag = drag;
        springJoint.connectedBody.angularDrag = angularDrag;
        Camera mainCamera = FindCamera();

		
        while(Input.GetMouseButton(0))
		{
            Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
            springJoint.transform.position = ray.GetPoint(distance);
			yield return null;
			print("hiffss " + Time.time.ToString());
        }
		
			
        if(Mathf.Abs(mousePos.x - Input.mousePosition.x) <= 2.0f  Mathf.Abs(mousePos.y - Input.mousePosition.y) <= 2.0f  Time.time - startTime < 0.2f  springJoint.connectedBody)
        {
            dir.y = 0.0f;
            dir.Normalize();
            springJoint.connectedBody.AddForceAtPosition(dir * pushForce, hitpoint, ForceMode.VelocityChange);
            ToggleLight( springJoint.connectedBody.gameObject );
        }


        if(springJoint.connectedBody)
        {
            springJoint.connectedBody.drag = oldDrag;
            springJoint.connectedBody.angularDrag = oldAngularDrag;
            springJoint.connectedBody = null;
        }
		
    }

    static void ToggleLight(GameObject go)
    {
        Light theLight = (Light)go.GetComponentInChildren(typeof(Light));
        if(!theLight)
        {
            return;
        }

        theLight.enabled = !theLight.enabled;
        bool illumOn = theLight.enabled;
        Component[] renderers = go.GetComponentsInChildren(typeof(MeshRenderer));
        foreach(MeshRenderer r in renderers)
        {
            if(r.gameObject.layer == 1)
            {
                r.material.shader = Shader.Find(illumOn ? "Self-Illumin/Diffuse" : "Diffuse");
            }
        }
    }

    Camera FindCamera()
    {
        if (camera)
            return camera;
        else
            return Camera.main;
    }

    void OnPostRender()
    {
        if(highlightObject == null)
            return;

        GameObject go = highlightObject;
        highlightMaterial.SetPass(0);
        Component[] meshes = go.GetComponentsInChildren(typeof(MeshFilter));
        foreach(MeshFilter m in meshes)
        {
            Graphics.DrawMeshNow(m.sharedMesh, m.transform.position, m.transform.rotation);
        }
    }


}

First, change DragObjects function from Void to iEnumerator.
and call this from a StartCorutine

Glad you were able to solve it, maxdelphi! Just an FYI, if you use [ code][/code] tags instead of quotes, it will be infinitely easier for people to read and comment on your code. :slight_smile:

Thanks, hey, please pass this code if they do not forget to say “Created by MAXDELPHI” hehehehe, and thanks for the advice "

 and [/ CODE]"

but this solution looked everywhere! and really strange (in a big way) that no one knew how to make this procedure.

curious ....