Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by doomprodigy · Dec 11, 2010 at 01:25 AM · coroutinecallcs1624

What am I doing wrong. "Calling Script to start coroutine"

void Update () {
      if (ptrScriptVariable.pistolammo = 0)
        {
            ptrScriptReload = (ReloadScript) objPlayer.GetComponent(   typeof(ReloadScript) );
            yield return StartCoroutine("Reload");
        }

I get the error Assets/Scripts/PistolScript.cs(18,14): error CS1624: The body of PistolScript.Update()' cannot be an iterator block becausevoid' is not an iterator interface type

Working with a C Sharp scipt when I am used to Java. Every other time I called a script this way to deduct ammo after shot in the Variable script worked.

Thanks..

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · Dec 11, 2010 at 01:26 AM 0
Share

Please place your script in the code format -- it really puts people off, and just annoying really :) (highlight your code and press the 0101 icon)

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Peter G · Dec 11, 2010 at 01:44 AM

You cannot yield in the Update(). Since Update is called everyframe, if you made one yield, then 2 would be called on top of each other, and so forth.

You can remove the yield return, and still start the coroutine, but then Unity will not wait until the execution finishes to move on. You could add a bool to ensure that the operation is finished; make sure that you don't start multiple coroutines at the same time as well.

bool reloading = false; bool finished = false;

void Update () { if (ptrScriptVariable.pistolammo = 0) { if(!reloading) { ptrScriptReload = (ReloadScript) objPlayer.GetComponent(typeof(ReloadScript)); StartCoroutine(Reload()); //This version is faster to call than a string.

          reloading = true;
      }
  }

  if(finished) {
      //Execute some code
  }

}

IEnumerator Reload () {

  while(stillReloading) {
      //Reload code;
      //I don't know what you are returning.
      yield return null;
  }

 reloading = false;
 finished = true;

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Best Answer

Answer by Eric5h5 · Dec 11, 2010 at 02:12 AM

It would be better if you removed Update; it's not serving any purpose. Just check that ptrScriptVariable.pistolammo is 0 when you decrement the variable, and call the coroutine from there. Checking ptrScriptVariable.pistolammo every frame is a waste, and you can't yield on coroutines in Update.

Edit: I'm not quite following what you're doing, but it seems like the game logic isn't really separated the way it should be ideally. Here's a quick example of a script, which would be attached to the player, that handles firing a bullet (I'm using JS here, sorry...I'm writing this quickly and C# is a pain in the ass for stuff like this). This won't directly apply to your project, but hopefully you get the general idea:

var bullet : Rigidbody; var bulletForce = 500.0; var maxAmmo = 6; private var ammoCount : int;

function Start () { ammoCount = maxAmmo;

 while (true) {
     if (Input.GetButtonDown("Fire")) {
         FireBullet();
         if (--ammoCount == 0) {
             yield Reload();
         }
     }
     yield;
 }

}

function FireBullet () { var bulletClone = Instantiate (bullet, transform.position, transform.rotation); bulletClone.AddForce (Vector3.forward * bulletForce); }

The input is in a coroutine in an infinite loop, so when it's yielding on Reload, it's not checking for input at all. Since the bullet is a rigidbody, you'd add a force once to make it move when it's instantiated, rather than using Translate in Update (which also won't work properly for collisions). Then the bullet prefab could have this script on it:

var maxFlightTime = 1.0; var bulletHit : GameObject;

function Start () { yield WaitForSeconds (maxFlightTime); DestroySelf(); }

function DestroySelf () { Instantiate (bulletHit, transform.position, Quaternion.identity); Destroy (gameObject);
}

function Update () { transform.position.y = 0.0; }

function OnCollisionEnter () { DestroySelf(); }

This way the bullet only cares about stuff that directly relates to it...the bullet is just going to fly until it runs out of steam or hits something; why should it care if the player is reloading his gun or anything?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image doomprodigy · Dec 11, 2010 at 01:43 PM 0
Share

Problem Solved, thanks for helping me understand (Peter and Eric), I did not end up rewriting my code I just was doing it in the wrong script and had to change a few things. SOLVED*

avatar image
0

Answer by doomprodigy · Dec 11, 2010 at 02:24 AM

Alirght here is my code without any modifications from my last post.

using UnityEngine; using System.Collections;

public class PistolScript : MonoBehaviour { private float moveSpeed = 30f; // how fast the bullet moves private float timeSpentAlive; // how long the bullet has stayed alive for private GameObject objPlayer; public float pistolammo = 10f; private VariableScript ptrScriptVariable; private VariableScript ptrScriptReload;

 // Use this for initialization
 void Start () {
     objPlayer = (GameObject) GameObject.FindWithTag ("Player");
     ptrScriptVariable = (VariableScript) objPlayer.GetComponent( typeof(VariableScript) );
 }

 // Update is called once per frame
 void Update () {
             ptrScriptVariable = (VariableScript) objPlayer.GetComponent( typeof(VariableScript) );
             if (ptrScriptVariable.pistolammo = 0)
     {
         ptrScriptReload = (ReloadScript) objPlayer.GetComponent( typeof(ReloadScript) );
         yield return StartCoroutine("Reload");
     }
     timeSpentAlive += Time.deltaTime;
     if (timeSpentAlive > 1) // if we have been travelling for more than one second remove the bullet
     {
         removeMe();
     }
     // move the bullet
         transform.Translate(0, 0, moveSpeed * Time.deltaTime);
         transform.position = new Vector3(transform.position.x,0,transform.position.z); // because the bullet has a rigid body we don't want it moving off it's Y axis
 }
 void removeMe ()
 {
     Instantiate(ptrScriptVariable.parBulletHit, transform.position, Quaternion.identity );
     Destroy(gameObject);
 }
 void OnCollisionEnter(Collision Other)
 {
     if ( Other.gameObject.GetComponent( typeof(AIscript) ) != null && Other.gameObject != objPlayer ) // if we have hit a character and it is not the player
     {
         AIscript ptrScriptAI = (AIscript) Other.gameObject.GetComponent( typeof(AIscript) );
         ptrScriptAI.health -= 10;
         ptrScriptVariable = (VariableScript) objPlayer.GetComponent( typeof(VariableScript) );
         ptrScriptVariable.pistolammo -= 1;
         Instantiate(ptrScriptVariable.parAlienHit, transform.position, Quaternion.identity );
         removeMe();
     }
     removeMe(); // remove the bullet if it has hit something else apart from an enemy character
 }

}

I understand about the Yield not going in the update that way because of the frames constantly updating. I need it to update to check what the ammount of ammo is so it knows if it needs to reload or not (That is why it is in update). I tried to work with the code posted by Peter.G but I could not get it to work right. All I want it to do is deduct ammo found in my variables (Which it does in a collision currently (Which I need to move to when it calls the time scale (I wish to do it her and not in the script which shoots as this is a base for other weapons) and after it has deducted all the ammo it instantly reloads which is just a waiting for a few secconds (In which I can incorporate Gui and animations later) after the time is over add the ammo back (so just a pistolammo += 10)

Heres the other scripts: Reload Script (So the wait time and where I will incorporate gui effects and animations later)

using System.Collections; using UnityEngine;

public class ReloadScript : MonoBehaviour { // C# Reload Coroutine IEnumerator Reload () { // Wait for one frame yield return 0;

// Wait for two seconds yield return new WaitForSeconds (5.0);

} }

And the variable script

using UnityEngine; using System.Collections;

public class VariableScript : MonoBehaviour { public float pistolammo = 10; public float shotgunammo = 6; public float assualtrifleammo = 30; }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image doomprodigy · Dec 11, 2010 at 02:25 AM 0
Share

Unsure if to make a new topic or not so I just gave another answer, Was also unsure if I typed it here or not so I did it as an awnser for the formating.

avatar image Eric5h5 · Dec 11, 2010 at 03:06 AM 0
Share

I edited my answer with some code to illustate.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Problem creating a cutscene event in c# 2 Answers

StartCoroutine important for using yield? 1 Answer

Coroutine cannot be automatically started from a static function 1 Answer

Is there a way to show a warning when a Coroutine is called directly? 0 Answers

WaitForSeconds is breaking out of my IEnumerator 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges