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 08, 2011 at 02:14 AM · arrayformultidimensional

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;
 
             }
         }

*

Comment
Add comment · Show 16
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 08, 2011 at 03:34 AM 0
Share

$$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...

avatar image IMTRIGGERHAPPY9 · Nov 08, 2011 at 03:40 AM 0
Share

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

avatar image jahroy · Nov 08, 2011 at 03:47 AM 0
Share

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];

avatar image IMTRIGGERHAPPY9 · Nov 08, 2011 at 04:11 AM 0
Share

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?

avatar image IMTRIGGERHAPPY9 · Nov 08, 2011 at 04:17 AM 0
Share

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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 7 · 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 12:08 AM 0
Share

tried changing to awake and changed it to - 200 in the execution order still null

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

this is very frustrating cause it should be working right?

avatar image IMTRIGGERHAPPY9 · Nov 09, 2011 at 12:13 AM 0
Share

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

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

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...

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

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?

Show more comments
avatar image
0

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?

Comment
Add comment · Show 22 · 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 01:25 AM 0
Share

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

avatar image jahroy · Nov 09, 2011 at 01:30 AM 0
Share

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...

avatar image IMTRIGGERHAPPY9 · Nov 09, 2011 at 01:33 AM 0
Share

how would i get that script any different?

avatar image jahroy · Nov 09, 2011 at 03:55 AM 1
Share

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.

avatar image jahroy · Nov 09, 2011 at 04:27 AM 2
Share

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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