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 Nk.Andrei · Aug 09, 2013 at 09:14 PM · errornullreferenceexceptiongridmonobehaviour

Problems creating a grid.I dont understand.

I need help in understanding why this code dosent work ..... if the class testt extends monoBehaviour and the script is attached to a plane it automatically creates a grid at runtime (the class uses the planes transform for the gridTransform) all good and fine but a warning always appears when I call the class from a different sctipt "You are trying to create a MonoBehaviour using the new keyword .This is not allowed......." and when I assign the transforms to the variables from the pathFind script it always says NullReferenceExeception but I cant figure out why ........ but the part that really bugs me is that if my class dosent extend Monobehaviour and I assign the gridTransform variable from a different script the cells dont show up(red rays shoot up from the cells position)

This is the pathFind script...most of the error and warnings show up because of this script

 #pragma strict
 import System.Collections.Generic;  ////the pathfinding script that initializes the GridCell
 
 var theOpenList     : List.<Cel> =new  List.<Cel>();
 var temporary         : Cel;
 
 
 
 
 var cube : Transform;
 var pref : Transform;
 
 var i : int;
 var x : int;
 
 var enemyTransf    : Transform;
 
 
 var theGrid : testt = new testt();///this gives the warnings
 
 
 function Start () 
 {
     distance = 10;
         
     theGrid.agentTransform = gameObject.transform;////this gives error
     if(enemyTransf != null)
     {
         theGrid.enemyTransform = enemyTransf.transform;////this gives error
     }
 
 
 
 
 }
 
 function Update () 
 {
     
     for(i = 0 ; i < theOpenList.Count ; i++)
     {
         
         if(theOpenList.Count > 0)
         {
             
             temporary = theOpenList[i];
             temporary.startSearch = true
             temporary.inList = true;
             Instantiate(cube,theOpenList[i].celPos,transform.rotation);
             temporary.cubeCheck = false;
             theOpenList[i] = temporary;
         }                        
 }
 


This is my testt class with witch I create the grid....if the class extends MonoBehaviour then the gridTransform variable will equal the plane the script is attached to and it always works........but if it dosent extend MonoBehaviour and the script is removed from the plane then the gridTransform is set from the theGrid script that is attached to the plane...

 #pragma strict
 
 class testt extends MonoBehaviour 
 {
     
     var gridTransform  : Transform;
     var agentTransform : Transform;
     var enemyTransform : Transform;
     
     
     var lowestCost     : float;
     
     var list : List.<Cel> = new List.<Cel>();
     
     
     var cube         : Transform;
     var hit         : RaycastHit;
     var distance     : int = 10;
     var color       : Color;
     
     var v             : int ;//number of cells
     var i             : int;
     var x             : int;
     var c             : int;
     var t            : int;
                 
     var cellSize     : int ;//cell size
     var cellSizeX     : int;
     var cellSizeZ     : int;
     
     var scale       : int;
     var cells         : Cel[,];//2d array ------ that ---- accepts only cells
     var cel         : Cel;
     
     var currentPos         : Vector3;
     var topLeftCorner     : Vector3;
     
     
     
     
     
     
     function Start()
     {
         
         gridTransform = gameObject.transform;///if the class extends MonoBehaviour then I use this////if not then I set the var from another script;
         
         lowestCost = Mathf.Infinity;
         v = 6;
         cellSize = 4;
         
         
         cel.obj      = agentTransform;
         cel.enemy     = enemyTransform;
         //cel.color     = Color.red;
         scale = 2;
         topLeftCorner =Vector3 (gridTransform.transform.position.x-gridTransform.transform.localScale.x/scale ,gridTransform.transform.position.y + 1  ,gridTransform.transform.position.z-gridTransform.transform.localScale.z/scale);
     
     
         cells = new Cel[v,v];///the size of the 2d array
         
         cellSizeX = gridTransform.transform.localScale.x / cellSize;//width
         cellSizeZ = gridTransform.transform.localScale.z / cellSize;//height
         
         
         for(i = 0 ; i < v ; i ++)
         {
             for(x = 0 ; x < v ; x ++)
             {
                             
                 ////THE CURRENT POSITION AND TYPE OF CELLS////
                 print(x);
                 currentPos = topLeftCorner + Vector3(i*cellSizeX,0,x*cellSizeZ); ////current position of the cell in i,x coordonates
                 cells[i,x] = cel;////each of cells of coordonates [1,1] or cells[1,2] etc will equal one cell
                 cells[i,x].celPos =currentPos;
                 
             }
         }    
     
     
     }

The last part is the theGrid script that is attached to the plane....

pragma strict

var grid : testt = new testt();

function Start () { grid.gridTransform = gameObject.transform; }

When I set the transform this way nothing appears;

I want to know a couple of things....

-why do the warnings appear and how do I get rid of them

-why the NullReferenceException appears in the agentTransform and enemyTransform and how do I resolve the problem

-why the rays dont appear if the class dosent extend monoBehaviour (I believe the rays dont appear because the Debug.DrawRay and the Physics.Raycast are a part of monoBehaviou ..correct me if I am wrong)

Lastly please explain to me what is causing these problems , how do I solve them and an explanation for each step.....I know I am asking a lot but anything is welcome.

Comment
Add comment · Show 2
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 gardian06 · Aug 09, 2013 at 09:42 PM 0
Share
  • we need to know what/where the warnings are to help you with them, and not all warnings are killers, but should still be taken care of.

  • where do these exceptions in the stack trace (in the console window there should be a list of lines referencing code similar to what shows up in an error message what is the top line of these NullRefereceExceptions.

  • Debug, and Physics are static classes accessible from anywhere

avatar image Nk.Andrei · Aug 10, 2013 at 09:17 AM 0
Share

The errors/warnings are displayed in the first attached script pathFind (they are commented out in red var theGrid and theGrid.agentTransform and theGrid.enemyTransform) ...the first one is the warning and the second two are the errors. Why dosent the class work if it dosent extend $$anonymous$$onoBehaviour?

0 Replies

· Add your reply
  • Sort: 

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

15 People are following this question.

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

Related Questions

Why am I getting NullReferenceException on my .Contains()? 1 Answer

Null Reference exception help 2 Answers

Referencing NON-monobehavior scripts - getting an error :/ 1 Answer

Calling 'AddItem' Method giving me error NullReferenceException: 0 Answers

[Closed]NullReferenceException on Object Instantiation onto game world 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