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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by MissV · Apr 17, 2014 at 03:53 PM · c#string

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;
 //}
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 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.

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 MissV · Apr 17, 2014 at 04:48 PM 0
Share

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

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Instantiate from correct Icon. 1 Answer

string.TrimEnd() not working as expected 1 Answer

How to split alphanumeric string into array of strings based from their type : alpha or numeric ? 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