Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by mrlafle · Mar 24, 2016 at 07:24 PM · arraysvariablesnewpublic

Simple Code Not Woking

This is just declaring variables, very basic stuff...

When I write this code

 public var word = new GameObject[wordLength];

I get these errors:

expecting '}', found 'public'

expecting EOF, found '}'

Can someone explain why this doesn't work? And possibly give an alternative that does? Thanks in advance!

Comment
Add comment · Show 2
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 LazyElephant · Mar 24, 2016 at 08:06 PM 0
Share

It looks like you probably have an extra curly brace somewhere. Post the rest of your code if you want more precise help.

avatar image mrlafle · Mar 24, 2016 at 10:27 PM 0
Share

here is my function... sorry about it being so complicated

 function BuildStage(){
     public var word = new GameObject[wordLength];    //the array of panels
     var matID: int;                    //tells what material to get based on what letter
     var bluOffset: int = 64;    //offset value to get blue letters from matID
     var redOffset: int = 38;    //offset to get red letters from matID
     
     //Instantiate Stage object
     Instantiate(stage, gameObject.transform.position, Quaternion.identity);
     //and call its Build function
     stage.GetComponent(StageScript).Build(wordLength);
     
     //this runs thru and sets both correct materials for every panel
     for(var i = 0; i < wordLength; i++){
         word[i] = Instantiate(panel, Vector3(i * 1.5, 0, 0), Quaternion.identity);
         
         word[i].GetComponent(PanelScript).myID = i;
         
         word[i].GetComponent(PanelScript).arrSize = wordLength;
         
         //setting prePanel and posPanel
         if(i > 0){
             //set this guy's prePanel...
             word[i].GetComponent(PanelScript).prePanel = word[i - 1];
             //and the last guy's posPanel
             word[i-1].GetComponent(PanelScript).posPanel = word[i];
         }
         
         matID = wordOne[i];
         word[i].GetComponent(PanelScript).matOne = matBank[matID - bluOffset];
         
         matID = wordTwo[i];
         word[i].GetComponent(PanelScript).matTwo = matBank[matID - redOffset];
     }
 }



1 Reply

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

Answer by LazyElephant · Mar 25, 2016 at 12:25 AM

Delete the "public" from public var word = new GameObject[wordLength];

Public is an access modifier used in class definitions and with class members/methods. You don't use it when declaring variables inside functions.

Also, I don't know if it's necessary since I always use c#, but it looks like the definition for the array should be var word : GameObject[] = new GameObject[wordLength];

Comment
Add comment · Show 2 · 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 mrlafle · Mar 25, 2016 at 12:38 PM 0
Share

Thanks for the help! I'm not sure if var word : GameObject[] = new GameObject[wordLength]; is the correct way to declare, but var word = new GameObject[wordLength]; certainly worked for me.

Now I just need to find out how to make the word variable accessible from another function in the script, because when I try that I get the error: 'word' is not a member of $$anonymous$$anagerScript.

The reason I need to declare the array within the function is because wordLength is null until just before the function is called. Declaring the var globally and then setting the array size later could be a work around, but I'm not sure how to do that either. Any suggestions?

avatar image LazyElephant mrlafle · Mar 25, 2016 at 06:05 PM 0
Share
 //outside the function
 var word : GameObject[];
 
 function BuildStage(){
 
 //inside the function
 word = new GameObject[wordLength];
 
 //...
 }

Also, if the answer is correct, don't forget to mark it correct by clicking the checkmark.

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

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

Related Questions

Can I have a variable accessed through the inspector and be accessed by a child script without making it public? 1 Answer

Public Arrays reset themselves 1 Answer

How can I make a variable store more different scripts? 0 Answers

Create "Optional" public variable that won't throw an error 1 Answer

Why UNITY Hates Me By Reading Array's Length! 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