I’m new to Unity and Javascript and quite frankly I’m having some problem with using classes in Unity. I thought I understood it but Unity is not responding in the way I expect so I must be missing something. I’ve tried many tutorials and spent hours searching but I still haven’t found any examples of what I am doing wrong or how I should do something instead.
I’ve successfully written a block moving game in Unity without using any specific classes and I’m now trying to expand it and thought using classes would give the extra functionality I am looking for.
I’ve created the class called Block to control each block. I have attached this script to the Block Prefab.
public class Block extends MonoBehaviour
{
//Variables for Block
var obj : GameObject;
var top : String;
var right : String;
var bottom : String;
var left : String;
var objID : int;
var texture : Texture2D;
}
function create(objID, x, y, top, right, bottom, left)
{
obj = GameObject.Instantiate(UnityEngine.Resources.Load("Block"), Vector3 (x, y, 0), Quaternion.identity);
var pepper = "Textures/B" + objID;
texture = UnityEngine.Resources.Load(pepper, Texture2D);
obj.renderer.material.mainTexture = texture;
objID = objID;
top = top;
right = right;
bottom = bottom;
left = left;
obj.name = 'block'+objID;
}
function colliderOff ()
{
obj.collider.enabled=false;
}
function rotateCW ()
{
Debug.Log('one');
var temp = top;
top = left;
left = bottom;
bottom = right;
right = temp;
Debug.Log('top = '+top+' left = '+left);
The initialisation script I have then creates a couple of blocks.
var block1 = new Block();
var block2 = new Block();
block1.create(1,1,1,"red","blue","null","null");
block2.create(2,0,1,"red","blue","blue","null");
I then have a line that modifies one of these blocks based on another function (colliderOff()) in the Block class.
block1.colliderOff();
This works fine, which is how I expected classes to work.
My main code (attached to the camera) then runs. I can select the block by mouse click and then move the block with the WASD keys, error checking etc. This is mainly taken from my first game and works fine.
The problem I have is when I try and run the rotateCW() function from the block class.
I’ve used the following snippet of code.
var myObject : Transform;
var selectedObject : GameObject;
\\ [on keypress] {
myObject.Rotate( 0,0,90);
selectedObject = GameObject.Find(myObject.name);
selectedObject.GetComponent(Block).rotateCW();
I was surprised that I couldn’t just use myObject.rotateCW() as that how I would have expected it to work. Eventually cobbled up the above script that not only rotates the cube, but should rotate the variables associated to that specific object.
The problem is while it appears to work and kicks off the debug logs lists in the function the variables are blank, so I’m not sure if it works or not. It’s like it runs the code but doesn’t recognise the object I am running it on as being already created.
What am I doing wrong?
Please let me know if you need more code or I need to explain something more clearly.
HOW I EXPECT IT TO WORK.
- Write Class
- Create Object
- Select Object
- Run function on object that is selected.
But something seems to be wrong between point 3 and 4.