- Home /
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.
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
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?
Your answer
Follow this Question
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