- Home /
Problem recognizing declaration of array of strings
Hello there, I've been trying to write a C# version of a simple JavaScript script and have run into an issue where the script is not compiling. It seems to be having trouble with the following line, specifically with string:
public string[] Tags = new string[0];
I've been searching for the answer on Unity Answers and many other places on the vast and wise internet and have tried many different ways of declaring this array of strings as well as different System includes and haven't had any luck. At this point I've spent a day and a half trying to find the solution and I'm reaching the end of what I can do without personally asking for help.
Also, please forgive me if the answer is obvious or I seem to be very ignorant on the subject. I'm in the first year of my programming course and have been working almost exclusively in C++ and python using visual studio. However, I am also a senior in a game art course and we've been using Unity. I am trying to use my basic programming skills to enhance what I've been working on, but am not very familiar with MonoDevelop and c# use in Unity. I apologize if I have missed something that may be common knowledge to the average user, or if I have trouble grasping some advice. I have some gaps in my general knowledge that sometimes cause explanations to go over my head.
Anywho, here is the entire script. The old JavaScript I was attempting to rewrite is commented out at the bottom if it helps. (Please note: I am not hoping to have someone write my script for me. The JavaScript is included in case it isn't clear as to what I was trying to do and that information would be useful. I'm very passionate about learning to be the best I can be, even if it is at a snails pace, so if someone is able to explain why what I've done isn't working and why something else will, I am very grateful.)
Thank you very much for taking the time to look at my question.
using System;
using System.Generic;
using System.Collections;
using UnityEngine;
public string[] Tags = new string[0];
public Tag (string TagX)
{
int i;
for(i = 0; i < tags.Length; i++)
{
public bool TempBool = TagX == Tags[i]);
if (TempBool)
{
return true;
}
else if (!TempBool)
{
return false;
}
}
}
// JAVASCRIPT
//var Tags : String[];
//function Tag (TagX : String)
//{
//for(var i=0;i<Tags.Length;i++)
//{
//var TempBool : boolean = (TagX ==Tags[i]);
//if(TempBool)
//{
//return true;
//}
//}
//return false;
//}
Answer by robertbu · Apr 17, 2014 at 04:09 PM
There are a number of problems in your script:
In C#, you must have your code inside a class.
As you mentioned, line 6 is a bit off. Since the variable is public, you want 'public string[] tags', and the actual size of the array and the individual entries would be set in the Inspector after the script is attached to a game object.
Typically you would declare 'i' inside the for() statement
You don't use 'public' for variables local to a function (line 13).
TempBool is not necessary (and wasn't necessary in the original code).
Lines 18 - 21 do not correctly translate the Javascrip logic. The idea is that you cycle through the array of tags. If any tag is equal, then return true. If after cycling, no tag match is found, return false.
By convention, variables start with a lower case letter. Sticking to this convention will help you with a variety of things as you move forward with Unity.
Putting it all together:
using System.Collections;
using UnityEngine;
public class MyTags : MonoBehaviour {
public string[] tags;
public bool Tag (string tagX)
{
for(int i = 0; i < tags.Length; i++)
{
if (tagX == tags[i])
{
return true;
}
}
return false;
}
}
Note that the class name and the file name must match in C#, so change the 'MyTags' class name to whatever you named your file.
Robertbu, Thank you ever so much. This was exactly what I was looking for. Your answer not only helped me sort out the issue that was immediately visible to me, but also helped me sort out and understand other problems that would cause me trouble in the future. You also lined out each issue that you saw and explained exactly why it should be addressed differently. Finally you put it all together so that I could have a solid visual reference as to the differences between the two scripts and how they are used. This not only gives me a better understanding of what I should be wary of when coding in the future, but it also highlights some of the gaps in my understanding and gives me new leads to research on my own to fill those gaps. Thank you again.
Also, I am not able to vote on your answer yet as I don't have enough reputation, so please accept this honorary upvote. d-(^_^)z