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 William_Goodspeed · Apr 28, 2014 at 05:50 PM · c#childchildrenweapon systemweaponchanging

Cycling Weapons, using C# int string problem

Hey folks,

So I'm working on a script that will allow me to cycle between my weapons. So far I've attached 1 empty object and 2 weapon objects to the main camera. And now I have written a script and attached it to the main camera which should help me cycle through them.

However the tutorial was in Js and I'm coding in C#. So I've got an error and I'm not really sure how to fix it. The console errors popping up at "cannot convert 'string' expression to type 'int'" Now I'm guessing this is where I'm calling my int index, but I'm not really clear on how to do it correctly. Guidance would be appreciated :)

 using UnityEngine;
 using System.Collections;
 
 public class WeaponSelect : MonoBehaviour {
     
 
     // Use this for initialization
     void Start () {
     
         SwitchWeapon (0);
 
     }
     
     // Update is called once per frame
     void Update () {
     
         if (Input.GetKeyDown ("1")) {
             SwitchWeapon("1");
                 }
 
         if (Input.GetKeyDown ("2")) {
             SwitchWeapon("2");
         }
 
     }
 
     void SwitchWeapon(index : int){
     
         for(var i = 0;i<Transform.childCount;i++)
         {
             if(i == index)
             {
                 Transform.GetChild(i).GameObject.SetActiveRecursively(true);
             }
             else { Transform.GetChild(i).GameObject.SetActiveRecursively(false);
         }
     }
 
 }
 }
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
0
Best Answer

Answer by wibble82 · Apr 28, 2014 at 05:59 PM

Hi there. There's a few issues with that code (I think its supposed to be c#!). I've fixed it up a little for ya:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponSelect : MonoBehaviour {
 
 
     // Use this for initialization
     void Start () {
 
         SwitchWeapon (0);
 
     }
 
     // Update is called once per frame
     void Update () {
 
         if (Input.GetKeyDown ("1")) {
             SwitchWeapon(1);
         }
 
         if (Input.GetKeyDown ("2")) {
             SwitchWeapon(2);
         }
 
     }
 
     void SwitchWeapon(int index){
 
         for(int i = 0;i<transform.childCount;i++)
         {
             if(i == index)
             {
                 transform.GetChild(i).gameObject.SetActive(true);
             }
             else 
             { 
                 transform.GetChild(i).gameObject.SetActive(false);
             }
         }
 
     }
 }

Main changes:

  • The definition of 'SwitchWeapon' was specifying the argument type javascript style. I've changed it to be correct c#

  • Your calls to SwitchWeapon in Update were passing in a string ("1") rather than just a number. c# doesn't auto convert like javascript, so you'd get errors reporting it couldn't convert from string to int.

  • I changed the references to 'Transform' to be 'transform'. The former references the class called Transform. The latter accesses the property of this mono behaviour called transform, which I'm guessing is what you want.

  • Again, I changed GameObject to gameObject on the same lines, as you want to access the gameObject property of the child, not the GameObject class.

  • SetActiveRecursively was deprecated a while back. SetActive now effectively does the same thing.

  • The for loop used a 'var' type. In c# you specify the type of the iterator explicitly, so I've made it an int.

Hope that helps :)

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 William_Goodspeed · Apr 28, 2014 at 06:45 PM 0
Share

Thank you very much :)

I actually managed to catch a few of these before your answer so I'm a little proud of myself hah! This was extremely helpful :)

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Loop to find a child in every children's child? 1 Answer

Switch for children not working 1 Answer

Multiple Cars not working 1 Answer

Get Children of Child and Deactivate/Activate on Demand 1 Answer

Distribute terrain in zones 3 Answers


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