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 negate · May 30, 2012 at 01:21 PM · variablereference

Reference variable

Hi, bit of background to the problem: I'm creating a board game and I'm mainly using javascript. Each piece can be anywhere on the board. I've created a script, attached to my floor called "startup" that holds all these global class variables. Just like chess they are A5, B9 etc.

Each unit also has a script attached to it, telling it what moves are possible. I'm however having difficulty updating my global variables after a move has happened.

i.e accessing the script attached to the floor when doing

startup.B9 works fine.

But I dont know in advance what position each unit is at so I added another variable to the script attached to each unit called : "current_pos"

And this is where i'm having a big problem!! Referencing startup.current_pos does not work as unity sees this entire call as a string. How can I force unity to see current_pos as variable that when I do startup.current_pos it will actually send startup.B9 or whatever position the unit is on to the startup script and not startup.current_pos

Any help would be greatly appreciated.

if (skuif4_att_check == "none"){ i = 0.0; while (i < 1.0) { i += 0.08; animation.CrossFade("CrouchRun"); transform.position = Vector3.Lerp (transform.position, move4, i); yield; }

      animation.CrossFade("Crouch");
      destroymove();
      move_check = "empty";
      current_pos = move4hit; 
 
          //1
      Debug.Log(current_pos);
          //2
      Debug.Log(startup.M6);
          //3
      Debug.Log(startup.current_pos);
          return;

1 = M6 2 = tank 3 = null

How can I go about fixing 3?

startup.js contains the following:

static var J1;
static var J2;
static var J3;
static var J4;
static var J5;
static var J6;
static var J7;
static var J8;
static var J9;
static var J10;
static var J11;
static var J12;
static var J13;
static var J14;
static var J15
etc....

Comment
Add comment · Show 11
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 whydoidoit · May 30, 2012 at 01:22 PM 0
Share

Could you post the code that you are using to make these calls?

avatar image negate · May 30, 2012 at 01:38 PM 0
Share

at work right now, will post it in less than 2 hours when i'm back home. But it's standard code. In the startup.js script I have static var A1; static var A2; etc.

on my unit.js I have var current_pos; and this variable does update correctly to A2 or A1 etc as the unit moves.

calling startup.A1 from the unit.js works as that variable does exist in the startup script. Calling startup.current_pos does not work as there is no such variable in the startup script. In PHP I did ".$varname." in a situation like the one above, but I have no idea how to do this in javascript

avatar image negate · May 30, 2012 at 04:26 PM 0
Share

added the code, hope it helps. The unity function which calls a scrip returns a string.

In php I was able to break the string with ".$varname." somehow I need to do this here

avatar image whydoidoit · May 31, 2012 at 09:54 AM 0
Share

Ok I'm a bit confused, it happens at my age :) Where do you define the current_pos variable - can you post that? The reason I ask is the way you have it would only work if it was a static (presu$$anonymous$$g startup is the name of a script). Can you also list the definition of $$anonymous$$6?

BTW if startup is the name of a script, following the convention of using an uppercase first letter makes these things easier to spot...

avatar image Loius · May 31, 2012 at 06:09 PM 2
Share

It sounds like you really want to be using arrays. Whenever you start having sequentially-labeled variables (A0,A1,A2,A3), it's usually a good idea to switch to an array ins$$anonymous$$d. Something like position_information(Letter, Number) is much more maintainable and accessible than having upwards of 10 separate variables and huge if/else blocks.

Unless I misunderstand. :)

Show more comments

2 Replies

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

Answer by whydoidoit · Jun 03, 2012 at 11:06 AM

@negate wants to access a variable position from a board he was modelling in a series of variables, where each variable represents a board position using chess like rank and file notation. The solution was not to use a different variable for each board position but make a dictionary and key into that instead.

You have two choices either use an array to hold your positions rather than lots of variables (that really isn't a good plan :) or you use a Dictionary which can have an index of "A3" "D4" etc.

Dictionary:

    import System.Collections.Generic;

    static var positions = new Dictionary<String, String>();

Now you can set positions like this:

    positions["A3"] = whatever;

And get them using

    var someVariable = startup.positions[current_pos];

One thing about dictionaries though is that you would need to initialise all of the values (you can't get out something you didn't put it).

That is different with an array:

     static var positions = new String[15,15];

But now your current_pos needs a rank and a file element - you could use a Vector2

     var current_pos : Vector2;

     current_pos.x  = 1;
     current_pos.y = 3;

     var someVariable = startup.positions[current_pos.x, current_pos.y];

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

Answer by whydoidoit · May 31, 2012 at 11:22 AM

startup is a script (class) and so to access the instance variables on it you need to get an instance of it.

If the startup script is attached to an object called "Floor" then you would do that like this:

  GameObject.Find("Floor").GetComponent(startup).current_pos = tag;
Comment
Add comment · Show 20 · 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 negate · May 31, 2012 at 03:56 PM 0
Share

nope it gives me the same problem.

Debug.Log(GameObject.Find("floor").GetComponent(startup).current_pos);

Returns:

$$anonymous$$issingFieldException: Field 'startup.current_pos' not found.

So its again not resolving the current_pos to A3 or whatever the value of curent_pos is.

I added a few lines from the startup.js at the main question.

I think I might now what is happening. current_pos is defined as string, in essence when I write GameObject.Find("floor").GetComponent(startup).current_pos, unity perhaps sees this :

GameObject.Find("floor").GetComponent(startup)."A3";

and of course "A3" does not exist, only A3 does, if this is the case how can I remove string qoutes?

avatar image whydoidoit · May 31, 2012 at 05:04 PM 0
Share

That isn't how it works! I really need to see the code that actually creates current_pos - how are you adding that? current_pos is a variable right? Or something else and I've missed the point entirely :)

avatar image negate · May 31, 2012 at 05:23 PM 0
Share

yes it's variable. Take for example this code which I use to deter$$anonymous$$e position of unit:

             a = 0;
        temprow = "Z";
       while (a < 15){
        temprow = alphaChars.Substring(a,1);
         if (temprow == row){
                move5coor =   alphaChars.Substring(a+1,1);  
                break;  
           } 
       a++;
       }
       move5hit = move5coor+move1num; 

move5hit for example will = "A4" or "A3" etc.

later in the script I check where the mouse has clicked then

current_pos = move5hit; or current_pos = move4hit;

etc

So throughout the script current_pos = a string value

avatar image whydoidoit · May 31, 2012 at 05:24 PM 0
Share

I still can't see where you do something like:

  var current_pos : String; 

In the class definition or somewhere else.

avatar image negate · May 31, 2012 at 05:58 PM 0
Share

All I do is this at the top of the script:

var default_pos = "A5"; var current_pos = default_pos;

Doesnt this define the current_pos as string?

I changed it to

var default_pos = "A5"; var current_pos : String; current_pos = default_pos;

Still gives me the same error:

$$anonymous$$issingFieldException: Field 'startup.current_pos' not found.

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I make reference/assocation to another component as variable? 0 Answers

Use Of Static Variables 1 Answer

C'# When Taking Variables From Other Scripts, The Values Do Not Update 1 Answer

How to call a function from another script - getting error "Object reference not set to an instance of an object" 5 Answers

Change value of integer based on string input in a separate script 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