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 IMTRIGGERHAPPY9 · Nov 09, 2011 at 04:47 AM · guivariablesreferencing

Reference specific variable from a specific gameObject?

ok so say i have a grid of like 100 cubes and i have a script that gets the object next to the previous grid that is then assigned to a variable. i then have a script that assigns a cube that is under my mouse to a variable how would i get the first variable from that certain cube? here are some scripts to help it make more sense

GuiStart.JS

 var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
       if ( Physics.Raycast( ray, hit, 100 ) ){
 
         if (hit.collider.CompareTag("gridFloor")){
 // gridPoint is the cube under mouse i was talking about!
            gridPoint = hit.collider.gameObject;
 
             }
         }

AIWalk.JS

 for(var i : int = 0; i < findTotalRows.gridX; i++)
 {
     for (var j : int = 0; j < findTotalRows.gridY; j++)
     {
         if(floorMap.map[i,j].name == this.name)
         {
             myX = i;
             myY = j;
         }
     }
     
 }
 xNext = myX + 1;
 yNext = myY + 1;
 xBack = myX - 1;
 yBack = myY - 1;
 
 tileUp = floorMap.map[myX,yNext];
 tileDown = floorMap.map[myX,yBack];
 tileLeft = floorMap.map[xBack,myY];

 tileRight = floorMap.map[xNext,myY]; 

these cubes are already put in an array called map[,] by the way

IMPORTANT all variables are showing up in inspector which means they are getting assigned correctly!!!

Comment
Add comment · Show 3
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 jahroy · Nov 09, 2011 at 05:05 AM 0
Share

One thing that makes it hard to understand your question is confusion about the meaning of the terms "grid" and "cube".

You say you have a "grid" of 100 "cubes" and a script that gets the "object" next to the "previous grid". This doesn't make sense to me.

Then you say you want to get "that first variable" from "that certain cube". I'm guessing that you mean the cube that was under the mouse, but what do you mean by "that first variable"?

I have a feeling that your issue is very simple, but your description of the issue has been the problem all along.

avatar image IMTRIGGERHAPPY9 · Nov 09, 2011 at 05:10 AM 0
Share

that first variable i am talking about the gird next to the previous grid, then by grid next to previous grid i mean if its right then + 1 to the x axis and if up + 1 to the y axis if its left - 1 from the x axis and if its down -1 from the y axis i already have it all set up with TILEUP, TILE DOWN, TILELEFT, AND TILERIGHT. i just need to be able to access the variables from the indivual gameObjects(GridPoints)

avatar image Jehos · Nov 09, 2011 at 05:13 AM 0
Share

I'm not sure what you mean or need, it's all very confusing...

However, here's a shot:

If you already have that "cube" under your mouse saved into a variable, use GetComponent (check the docs) on that variable to get the instance of the script that has the variables you which to access.

I'm assu$$anonymous$$g the "cube" you're getting is a GameObject (or a Transform, or a Script or something that has a Unity GameObject). Specifically, I assume the "cube" you're getting is not a simple "int" (i.e.: ID) of a "cube".

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jahroy · Nov 09, 2011 at 05:15 AM

Based on what I think I understand about your issue, I would recommend this (again):

  • Attach the script named GuiStart to an object in your scene named FunkyObject

  • Add the following to the top of AIWalk.js:

    public var guiStartScript : GuiStart;

  • Attach AIWalk to some object in your scene

  • Select the item AIWalk is attached to by left-clicking it in the Hierarchy

  • Look at the Inspecter and observe a slot named Gui Start Script

  • Drag FunkyObject (the item we attached GuiStart to) onto the slot named Gui Start Script in the Inspector

  • You will now be able to access GuiStart's variables and functions in the script named AIWalk.js like this:

    if ( guiStartScript != null ) {
        printInfo(guiStartScript.gridPoint.tileRight);
    }
    

    printInfo is a hypothetical name for a function that would "do stuff" to gridPoint.tileRight...

    By adding the above line to your AIWalk script, you say this to the compiler: This script contains a reference to another object that has a GuiStart script attached to it.
    I can now do whatever I want with the functions and variables associated with that object's GuiStart script.

    Comment
    Add comment · Show 5 · 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 IMTRIGGERHAPPY9 · Nov 09, 2011 at 05:23 AM 0
    Share

    my game Object(Controller) that has guistart attached to it, and the object(TileFloor) that has aiwalk wont let me drag and drop it into that slot,

    i mean tile floor wont let me drag and drop controller onto guistartscript in the inspector

    avatar image jahroy · Nov 09, 2011 at 05:27 AM 0
    Share

    You want to drag an Object that has a GuiStart attached to it ONTO the GuiStart slot on your TileFloor object.

    Are both objects in the Hierarchy and enabled?

    avatar image IMTRIGGERHAPPY9 · Nov 09, 2011 at 05:28 AM 0
    Share

    TileFloor isn't in the hierarchy because it gets instantiated in a script its just in my assets right now.

    avatar image jahroy · Nov 09, 2011 at 05:37 AM 0
    Share

    $$anonymous$$y brain is going to fall out of my ears trying to understand what you're talking about...

    avatar image IMTRIGGERHAPPY9 · Nov 09, 2011 at 05:40 AM 0
    Share

    lol i agree..... but the way i created my grid was with a script so my tile floor isnt in the hierarchy until the game has been started

    avatar image
    0

    Answer by IMTRIGGERHAPPY9 · Feb 20, 2014 at 06:29 AM

    Ha ha ha.... looking back at these old questions gives me a good laugh at how far i have come! still working at unity3D and loving every minute of it thanks to great tools like this and the awesome community surrounding it!

    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

    4 People are following this question.

    avatar image avatar image avatar image avatar image

    Related Questions

    How to group variables in the Inspector? 5 Answers

    GUI player menu is not updating values (score, exp, HPs) 1 Answer

    Instance(?) problems 1 Answer

    Why does it give me an error in this small java script for sprint? 1 Answer

    C# Using Variables for GUI Rectangles 0 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