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 jadaith · Sep 23, 2013 at 02:13 AM · c#javascript

Problem with referencing gameobjects in a if statement

I'm following a tutorial that's in javascript and I'm converting the code over to C# and I'm running into a problem at the end of the tutorial. The problem I'm having is running into this line of code:

for(theMuzzlePos in muzzlePositions) { Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation); Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation); }

C# apparently does not recognize "in" in the conditional statement. What can I do to change this and reference those gameobjects?

 using UnityEngine;
 using System.Collections;
 
 public class Turret_Script : MonoBehaviour {
     
     public GameObject myProjectile;
     public float reloadTime = 1f;
     public float turnSpeed = 5f;
     public float firePauseTime =.25f;
     public GameObject muzzleEffect;
     public float errorAmount = .001f;
     public Transform myTarget;
     public Transform[] muzzlePositions;
     public Transform turretBall;
     
     private float nextFireTime;
     private float nextMoveTime;
     private Quaternion desiredRotation;
     private float aimError;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(myTarget)
         {
             if(Time.time >= nextMoveTime)
             {
                 CalculateAimPosition(myTarget.position);
                 turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
             }
             if(Time.time >= nextFireTime)
             {
                 FireProjectile();
             }
         }
     }
     
     void OnTriggerEnter(Collider other )
     {
         if(other.gameObject.tag == "Enemy")
         {
             nextFireTime = Time.time +(reloadTime*.5);
             myTarget = other.gameObject.transform;
         }
     }
     
     void OnTriggerExit(Collider other)
     {
         if(other.gameObject.transform == myTarget)
         {
             myTarget = null;
         }
     }
     
     void CalculateAimPosition(Vector3 targetPos)
     {
         var aimPoint = new Vector3(targetPos.x+aimError, targetPos.y+aimError, targetPos.z+aimError);
         desiredRotation = Quaternion.LookRotation(aimPoint);
     }
     
     void CalculateAimError()
     {
         aimError = Random.Range (-errorAmount, errorAmount);
     }
     
     void FireProjectile()
     {
         audio.Play();
         nextFireTime = Time.time+reloadTime;
         nextMoveTime = Time.time+firePauseTime;
         CalculateAimError();
         
         for(theMuzzlePos in muzzlePositions)
         {
             Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
             Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
         }
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by syclamoth · Sep 23, 2013 at 02:16 AM

The issue here is a slight difference in the syntax for collection iterators in C#. You should instead be using

 foreach(Transform theMuzzlePos in muzzlePositions) {
     // blah blah blah
 }
Comment
Add comment · Show 3 · 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 jadaith · Sep 23, 2013 at 03:24 AM 0
Share

thank you that was exactly it. I've never ran into in before in if statements so I was completely clueless.

avatar image syclamoth · Sep 23, 2013 at 03:29 AM 0
Share

Also, this is a for-loop. If-statements work pretty much the same way in C# as they do in Unityscript.

avatar image jadaith · Sep 23, 2013 at 04:37 AM 0
Share

Ah, your absolutely right sorry wasn't paying attention.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

script resources 2 Answers

Instantiate into array : Out of range? 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