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;
}
}
}
answered
Sep 16 '11 at 02:03 AM
stereosound
33
●
5
●
8
●
9
It's called an "Exception". Not an "Expection". ;-) Can you take a look at the error that appears in the editor's console to see which line of code causes the exception? The line number appears in the stacktrace that comes with the error; use that to find the line, then highlight that for us. It makes it easier to help you.
As a quick first guess, it might be because you don't have a main camera. Notice that you code accesses Camera.main. This property returns the first camera it finds tagged "MainCamera". If there is no such camera in your scene, it returns null, and then, calling ScreenPointToRay results in the NullReferenceException.
Thanks for taking the time to answer Christian. It was on the yield MoveObject.use.Translation, line, and I actually figured it out about five minutes after I posted this. Funny how you can have a problem for over a day, then the moment you ask for help it dawns on you?
My issue was MoveObject.js wasn't attached to an object; the object it was attached to got accidentally deleted as a result of bad coding that I tried to troubleshoot before. I was trying to do:
var transform : Transform = hit.collider.GetComponent(Transform); if (transform) {
Instead of just:
Turns out in trying to bugfix this I deleted my object, then when I came up with a better method it still didn't work as a result.
At the very least, perhaps this will give someone an idea of how to zoom-in on instantiated objects. My "create" code is something like this:
function Start () { for(var i = 0; i < 5; i++) var object = Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity); object.name = "Object" & i } }
The prefab has the above script attached, with MoveScript.js attached to another object.
You should post this as an answer to your own question and have someone voting for it.
Agreed - Good you got it fixed!