- Home /
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!!!
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.
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)
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".
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.
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
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?
TileFloor isn't in the hierarchy because it gets instantiated in a script its just in my assets right now.
$$anonymous$$y brain is going to fall out of my ears trying to understand what you're talking about...
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
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!