- Home /
Models don't work as a gameObject in my script, i'm confused! Help!
have a brain model, i need to make click in each part of this and show a GUI with text, here is the code embed to a "model" who does not but ir works in capsules, cubes etc.* Character- using UnityEngine; using System.Collections;
public class Character : MonoBehaviour { public string myWords = string.Empty;
private void Start ()
{
}
private void Update ()
{
}
public string GetWords ()
{
return myWords;
}
}
now this is the code for the MainCamera who allows you to click a gameobj and showing text from GetWords string and if you click outside of any gameObject this UI closes.
UI_Demo- using UnityEngine; using System.Collections;
public class UI_Demo : MonoBehaviour
{
private GameObject clicked = null;
private void Start()
{
}
private void Update ()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(Camera.mainCamera.ScreenPointToRay(Input.mousePosition), out hit))
{
if (hit.collider.gameObject.GetComponent<Character>() != null)
clicked = hit.collider.gameObject;
else
clicked = null;
}
else
clicked = null;
}
}
private void OnGUI()
{
if (clicked != null)
GUI.Label(new Rect(15,15,200,25), clicked.GetComponent<Character>().GetWords());
}
}
thy thing is that the script works only in gameObj and i'm a n00b ¿how can i make a imported FBX a gameObject? or why i can't make the script work right if i attach it to a imported model? )':
Why don't you define your game object as public and define it in the inspector?
Answer by robertbu · Jul 30, 2013 at 11:23 PM
You likely need a mesh collider added to your model. Select the game object in he hierarchy and then do Component > Physics > Mesh Collider.
Next is to figure out what is happening with your Raycast. Just above line 18:
Debug.Log(hit.collider.name+", "+hit.collider.tag);
This will see if you are hitting anything, and if so, if it is what you expect. If it is hitting he game object you expect, then check to see if the GetComponent() call is failing.
If the debug does not print at all, remove the mesh collider and replace it with a sphere collider to debug the situation. If the sphere works but the mesh does not, you may have to make changes to your model.
thy colider helpd!!!!! can you keep helping me in my proyect via chat )':?
Your answer
