- Home /
Please help with beginner Prefab & OnMouseDown
Hello, I am working on a card game for the web. I have four prefabs for the backs of the cards: blue, red, yellow, green. Right now I can push play and the script instantiates a blue card, but I can't figure out how to register a click on this card.
Attached to the main camera, instantiate_prefabs.js:
var blue_card : GameObject;
function Start () { var blue_card = Instantiate(blue_card,
Vector3 (0, 0, 0), Quaternion.identity);
}
Attached to each of the four prefabs for blue_card, red_card, yellow_card, green_card, flip_card.js:
function OnMouseDown() {
Debug.Log("Prefab instance clicked.");
}
Sorry if this code is weird, I am baffled. Each card is made of a UV Plane, courtesy of a graphic artist, so please go easy as I don't know anything about 3D art. Thank you so much.
Answer by efge · Feb 21, 2011 at 11:40 PM
Is there a collider attached to the prefabs?
I made a new prefab using 'generate colliders', but I'm not sure how to associate the collider with On$$anonymous$$ouseDown...
I'm confused. Why do I have to add the prefab to the script itself as well as onto the main camera where the script is attached?
I think for your cards a simple BoxCollider does the best. Add "Component -> Physics -> Box Collider" and scale it to the size of the card. (Do not make it flat like your planes, just give it some height.)
Your answer