- Home /
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);
}
}
}
}
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 :)
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
Follow this Question
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