- Home /
Why Is the game object not selected? I'd like to understand why.
using UnityEngine;
using System.Collections;
public class Selectable : MonoBehaviour {
bool Selected;
// Use this for initialization
void Start () {
Selected = false;
}
// Update is called once per frame
void Update () {
if (Selected) {
if (Input.GetKeyDown(KeyCode.Escape)){Selected = false;}
Transform selectionDisk;
selectionDisk = Instantiate(selectionHalo, transform.position, transform.rotation) as Transform;
}
}
void OnMouseDown(){
Selected = true;
}
}
The Script gets attached to any game object that I want to be able to select. So far the screen just looks at a cube, but the cube doesn't do anything on click. And now the compiler says the prefab for selection halo doesnt exist. I added a render call to change the color instead, but still no work.
Does it have a collider attached? How is it set up? What is your scene setup like?
i don't see any OnCollision or OnTrigger functions here...i'm not really an expert but how exactly does your selection work? if you can tell us that maybe we can understand the problem better...
From what we can see, nothing calls On$$anonymous$$ouseDown.
On$$anonymous$$ouseDown is a monobehaviour function, it need not be called. what he is missing is a collider I guess.
Check if selectionHalo object has some kind of collider attached to it.
Thanks for the responses, but both the cube this script attaches to and the selection halo I am instantiating both include colliders as part of the game objects.
Answer by MalachiteBR · Aug 26, 2015 at 12:59 AM
According to: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
"This function is not called on objects that belong to Ignore Raycast layer.
OnMouseDown can be a co-routine.
This event is sent to all scripts attached to the Collider or GUIElement."
Just make sure you are not missing anything, often when these mysterious problems occurs we probably are missing something.
PS. And try to make all your methods/functions private or public, according to your needs.
Answer by kylebarker82 · Aug 26, 2015 at 01:28 PM
Rigidbody or a Collider is attached I assume. If so you can try OnMouseEnter() to debug in the console and debug the OnMouseDown() with the bool value of Selected. Also double check you have an EventSystem gameobject in the scene. this usually helps the onmousedown issues for me. Double check that the colliders are set as 3D or 2D depending on your scene setup.
also it looks like you're casting the instatiate as a transform and not a gameobject, if you're moving a game object, then move the prefab of the gameobject , by it's transform in the form of a gameobject, not make a duplicate transform to move the duplicate transform wherever you want. did that make sense. Try casting it as a gameobject, not a transform.