I get the NullReferenceException when trying to use the MoveObject.js script here: unifycommunity.com
Basically, I instantiate 5 objects (object0, object1, etc.) then creating a “zoom” script with the following code; unfortunately, I can’t get it to move.
function Start()
{
StartCoroutine("CoStart");
}
function CoStart() : IEnumerator
{
while (true)
yield CoUpdate();
}
function CoUpdate() : IEnumerator
{
var hit : RaycastHit;
var itsaray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay (itsaray.origin, itsaray.direction * 200, Color.yellow);
if (Physics.Raycast (itsaray, hit) && Input.GetMouseButtonDown(0) && !isMoving) {
if (name == hit.collider.gameObject.name) {
var transform2 : Transform = hit.collider.GetComponent(Transform);
yield MoveObject.use.Translation(transform, Vector3(15.5, 26.75, 15), 1, MoveType.Speed);
}
}
}
I’m starting to think it’s an issue with the MoveObject.js script that cannot take the transform reference. Is this the case?
I fixed the problem; through some bad debugging I accidentally deleted the object that MoveObject.js was attached to, thus the script couldn’t be called. Adding it to any object (mine is on an empty gameobject) will make this script work.
For reference for anyone who is trying to create a zoom-in on cards:
ATTACH TO EMPTY GAMEOBJET:
function Start () {
var card_arr : Array = new Array();
for (var z = 1; z < 7; z++) {
// 7 is arbitrary deck size + 1
card_arr.Add(z);
}
deckSize = card_arr.length;
for (var x = 0; x < decksize; x++) {
var card_made = Instantiate(card_prefab, Vector3 (x*0.7, 0, 0), Quaternion.identity);
card_made.transform.Rotate(Vector3 (0, 180, 0));
//rotated because the material applies upsidedown
card_made.name = "object"+x;
card_made.tag = "object"+x;
var choice = Random.Range(0,card_arr.length);
// this changes the picture of the card to the correct reference
card_made.renderer.material.mainTextureOffset = Vector2 (card_arr[choice]*0.1, 0);
card_arr.RemoveAt(choice);
}
}
This is then attached to the prefab:
function Start()
{
StartCoroutine("CoStart");
//semi-cheating, this is just the position on camera that a normal sized object will take up the full screen, playing with perspective rather than scaling
endPos = Vector3(15.5,26.75,15);
}
function CoStart() : IEnumerator
{
while (true)
yield CoUpdate();
}
function CoUpdate() : IEnumerator
{
var hit : RaycastHit;
var itsaray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (itsaray, hit) && Input.GetMouseButtonDown(0)) {
if (name == hit.collider.gameObject.name && Card_Create.CARDZOOMED == zoomed) {
//There is probably a more sophisticated way to toggle booleans
if (Card_Create.CARDZOOMED) {
Card_Create.CARDZOOMED = false;
}
else {
Card_Create.CARDZOOMED = true;
}
if (zoomed) {
zoomed = false;
}
else {
zoomed = true;
}
startPos = transform.position;
yield MoveObject.use.Translation(transform, transform.position, endPos, 125, MoveType.Speed);
//Switches startPos and endPos
transPos = endPos;
endPos = startPos;
startPos = transPos;
}
}
}