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 koo_04 · Mar 26, 2012 at 03:01 PM · javascriptarrayclasses

Accessing variable from a class

I have a Class which contains multiple variables that are set at Start(). Start() Contains a loop to set those variables multiple times from an array. So.. this is my code currently

             for (var x=0; x<gameCount; x++){
                 var num : int;
                 var dataArr = new Array();
                 var d = ""+gameArray[x];
                 dataArr = d.Split(":"[0]);
 //                Debug.Log("ToString Data: " + d);
 //                Debug.Log("dataArr Data; " + dataArr);
                 var data = new game();
                         data.id = int.Parse(dataArr[0]);
                         data.turn = int.Parse(dataArr[1]);
                         data.user1 = int.Parse(dataArr[2]);
                         data.user2 = int.Parse(dataArr[3]);
                         data.base1 = dataArr[4];
                         data.base2 = dataArr[5];
 //                        Debug.Log(data.user1+" vs "+data.user2);
                 gameArray[x] = game;
 //                Debug.Log("From gameList " + data.id);
         }
 }
 
 public class game extends MonoBehaviour{
     var id : int;
     var turn : int;
     var user1 : int;
     var user2 : int;
     var base1 : String;
     var base2 : String;
 }

As you can see, I put in plenty of debugs in there to help me out. Everthing here works. I can even debug the variables from this class and I get the needed values. My biggest problem is that I can not seem to figure out how to properly access these from another script. Am I gonna have to set more then one class for every irritation of that loop? The idea behind this process was so I can do a loop somewhere else to get what I need out of that array. I was attempting to do 'game.gameArray[x].id' and so on. Obviously it was telling me gameArray was not part of class 'game'. So... what is the right way of doing this??

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 koo_04 · Mar 26, 2012 at 03:44 PM

I have figured out what I SHOULD be doing. I instead made the variables in the class arrays and accessed them that way. Making them static on top of that made it accessible in another script.

Isn't it just awesome that you go and ask a question, then just after that think of the solution? xD Man programming is fun.

Here is my solution:

for (var x=0; x < gameCount; x++){

var num : int;

var dataArr = new Array();

var d = ""+gameArray[x];

dataArr = d.Split(":"[0]);

var data = new game();

   data.id.Push(int.Parse(dataArr[0]));

   data.turn.Push(int.Parse(dataArr[1]));

   data.user1.Push(int.Parse(dataArr[2]));

   data.user2.Push(int.Parse(dataArr[3]));

   data.base1.Push(dataArr[4]);

   data.base2.Push(dataArr[5]);

gameArray[x] = game;

}

} //this is the end of the function

public class game extends MonoBehaviour {

static var id = new Array();

static var turn = new Array();

static var user1 = new Array();

static var user2 = new Array();

static var base1 = new Array();

static var base2 = new Array();

}

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Array of custom class objects all return the same value? 1 Answer

Trying to create an array within an array (Round 2) 1 Answer

How to use Javascript Array with classes? 1 Answer

Make an array containting multiple classes 1 Answer

Access a variable of a class stored in an array. 2 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