- Home /
print(var a = object) = null while print(oject) = what i want
Why isn't this code working when i try to print out tileRight it says null
its all in start function so i can declare all this before the game starts, they are all pretty much global vars to
var monsterOnMe : GameObject;
var tileRight : GameObject;
var tileUp : GameObject;
var tileLeft: GameObject;
var tileDown : GameObject;
var floorMap : GridMaker;
var myX;
var myY;
var xNext: int;
var yNext: int;
var xBack: int;
var yBack: int;
var canPlace : boolean;
var getGridPoint : GuiStart;
function Start ()
{
canPlace = true;
// see if towers can be placed on that gridfloor
getGridPoint = GameObject.Find("Controller").GetComponent(GuiStart);
//Make Sure Controller is always called Controller!!!!
var findTotalRows = GameObject.Find("Controller").GetComponent(GridMaker);
floorMap = GameObject.Find("Controller").GetComponent(GridMaker);
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];
}
hope this makes more sense now.
GridMaker.JS
var instantiateObject : GameObject;
var gridX = 10;
var gridY = 10;
var map : GameObject[,];
function Start () {
map = new GameObject[gridX,gridY];
for ( var x = 0; x < gridX; x++){
for (var y = 0; y < gridY; y++){
var myName = Instantiate(instantiateObject, Vector3(x *.5,0,y * .5), Quaternion.identity);
myName.name = "x: " + x + "y: " + y;
map[x,y] = myName;
//Debug.Log(map[x,y]);
}
}
}
GuiStart.JS
var gridPoint : GameObject;
// do raycast
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if ( Physics.Raycast( ray, hit, 100 ) ){
if (hit.collider.CompareTag("gridFloor")){
gridPoint = hit.collider.gameObject;
}
}
*
$$anonymous$$y guess is the problem is in code that you're not sharing.
The code you have shared makes some assumptions about the contents of floor$$anonymous$$ap. Are you sure it's initialized and contains the expected data?
The first thing I would do is add some debug statements... $$anonymous$$ake sure myX and myY are getting set when you want them to and make sure floor$$anonymous$$ap.map[xNext, j] is what you expect it to be.
Is floor$$anonymous$$ap a two-dimensional array or a hash map? Also, are you sure the contents of the map can be casted to an object that contains a GameObject? I would test this code line by line with good old fashioned debug statements...
i already checked i and j and they are working its the part where i try to get the tile to the right and the array is a bunch of instantiated gameOBjects so it should work
So, if you add Debug.Log(floor$$anonymous$$ap.map[xNext,j].name to your code, you see the output that you expect?
$$anonymous$$aybe I'm misunderstanding something, but how can you have GameObject (with capital G, as in the name of the class) as a member/property of floor$$anonymous$$ap.map[i,j]? That seems very strange.
GameObject (with a capital G) refers to the GameObject class, not an instance of a GameObject. If floor$$anonymous$$ap.map is supposed to be an array of GameObjects (hard to know for sure, since you don't tell/show us), you should set tileRight equal to one of its elements (again, assu$$anonymous$$g it's supposed to be a GameObject). Something like this:
var tileRight : GameObject;
tileRight = floor$$anonymous$$ap.map[xNext,j];
ok so print myY is right and xNext is right, and then myY and myX in the array like this map[myX, myY] is right, but when i go map[xNext,myY] it says null... wtf?
oh my bad it is working i just say the out of range comment when it got past its array... fail sorry thanks for the help trouble shooting it, i dont know what i would do without this site
Answer by syclamoth · Nov 08, 2011 at 08:07 AM
I think I can see the problem here- do you think maybe the Start functions aren't getting called in the right order? It isn't necessarily defined what order Start functions happen in unless you manually specify it in the 'Script Execution Order' settings.
Are your values dependent on something which happens in the GridMaker's start function? If so, try changing the GridMaker's 'Start' to 'Awake' to make sure it executes first. You can also set it up in the script exectuion order settings, which I mentioned earlier. These can also fix problems which arise in Update for similar reasons.
tried changing to awake and changed it to - 200 in the execution order still null
this is very frustrating cause it should be working right?
so when i print this:
print(floor$$anonymous$$ap.map[xNext,myY]);
it comes out correct, but when i print this:
print(getGridPoint.tileRight);
it comes out null.... so the problem is happening when i assign the var, but i dont understand why
It is null because you are using GameObject.Find() to set getGridPoint.
This seems like a bad idea and doesn't appear to be working.
I don't understand why you would do this...
thats so i can get the GameObject that is holding my script that creates the grids how is it a problem? i need it to get that script?
Answer by jahroy · Nov 09, 2011 at 01:18 AM
Why are you using GameObject.Find() to assign your variables?
That seems like a bad idea to me... My guess is you do not want to do this.
Even if you did want to use this approach, it looks like you're doing it wrong.
This line looks questionable:
getGridPoint = GameObject.Find("Controller").GetComponent(GuiStart);
Here's how I would interpret that code:
Look for a GameObject in the scene with the name "Controller". If there is such a GameObject, check to see if it has a script named GuiStart attached to it. If it does, return a reference to that script. If it does not, return null.
Even if your script IS attached to a GameObject that has a script named GuiStart attached to it, I think your script would fail. My guess is your trying to do too much conversion between incompatible types in one step... In my humble opinion that line of code is ALL bad. You're trying to do too many things in one step and you never test if any of them succeed. Beyond that I would argue that there is no reason to use GameObject.Find() to begin with...
It seems to me that it would be much easier to assign your variables using the Inspector...
Wouldn't it be easier to assign getGridPoint by dragging and dropping a GameObject (that has a GuiStart script attached to it) in the Inspector?
girdPoint is always being updated with your mouse over, so if i mouse over a certain point it will tell me the name of that instantiated grid point, ill put my grid instantiation script up here so you can see what i am talking about, and girdPoint isn't the problem because i have like 3 other references that are printing out the right name
I still don't see any reason why you need to use GameObject.Find().
Are you sure that GameObject.Find("Controller").GetComponent(GuiStart) ever returns a non-null value?
I have a feeling you're not reading anything I'm writing...
It's going to be hard to help without seeing more of your code at this point. The issue has to exist in code you haven't shared.
In the code that you DO share, getGridPoint only gets set in one place: in the Start function. You set it to the result of GameObject.Find and it never changes after that (based on what we can see). getGridPoint.tileRight must start as null or get set to null by some code that you haven't shared.
I also find your use of the term "global var" to be alar$$anonymous$$g. What do you mean by global var? The variable named getGridPoint is a class variable. Every GameObject that has the above script attached to it has its own copy of all the variables that you define at the top of your script.
I'm really trying, but I think that's too much random code for me to process...
I don't want to sound critical, but I think there is evidence in your code that you might not understand how objects and scripts interact with each other in Unity.
I still think the use of GameObject.Find is highly questionable... Even more so now that we see it used inside the Update function.
I'll ignore that for now...
The first thing I would do would be to add debug statements. Better yet, I would add a label somewhere on the screen that displays a bunch of debug info. I might add something like this to GuiStart.js:
var debugRect : Rect = Rect(300, 300, 250, 150);
function OnGUI ()
{
var debug$$anonymous$$sg : String = "";
if ( gridPoint ) {
debug$$anonymous$$sg += "gridPoint: " + gridPoint.name;
}
debug$$anonymous$$sg += "\n";
if ( gridPoint.tileRight ) {
debug$$anonymous$$sg += "tileRight: " + gridPoint.tileRight;
}
GUI.Label(debugRect, debug$$anonymous$$sg);
}
To get to the bottom of this situation, I would probably add many such labels containing all kinds of info. I guess the real approach would be to actually use a debugger :o
Your answer
Follow this Question
Related Questions
How to Set a Multidimensional Array Length 1 Answer
What am I overlooking? -1 Answers
2D array in shader 0 Answers
how to find if all objects in array are missing 1 Answer
Problem with nested for loops 2 Answers