using UnityEngine;
using System.Collections;
public class ChangingRoom : MonoBehaviour {
private int _charModeIndex = 0;
private CharacterAsset ca;
private string _charModeName = "x6";
private GameObject model;
// Use this for initialization
void Start () {
ca = GameObject.Find("Character Asset Manager").GetComponent<CharacterAsset>();
InstantiateCharacterModel();
}
void OnGUI(){
ChageCharacterMash();
if(GUI.Button( new Rect( Screen.width * .5f - 95, Screen.height * 5f , 30, 30), "<")
model.transform.Rotate(Vector3.up * Time.deltaTime * 100);
if(GUI.Button( new Rect( Screen.width * .5f + 65, Screen.height * 5f , 30, 30), ">")
model.transform.Rotate(Vector3.down * Time.deltaTime * 100);
}
private void ChageCharacterMash(){
if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height - 35,120,30),_charModeName)){
_charModeIndex++;
InstantiateCharacterModel();
}
}
private void InstantiateCharacterModel(){
switch(_charModeIndex){
case 1:
_charModeName = "camaro";
break;
default:
_charModeIndex = 0;
_charModeName = "x6";
break;
}
if(transform.childCount > 0)
for(int cnt = 0; cnt < transform.childCount ; cnt++)
Destroy(transform.GetChild(cnt).gameObject);
GameObject model = Instantiate(ca.characterMesh[_charModeIndex],transform.position,Quaternion.identity) as GameObject;
model.transform.parent = transform;
model.transform.rotation = transform.rotation;
}
}
4 Answers
4You're missing a closing parenthesis at the end of each of the button lines in OnGUI.
Add in an ) at the end of each of those lines and it should fix it
using UnityEngine;
class Test02 : MonoBehaviour
{
private GameObject model;
private string _charModeName;
private int _charModeIndex;
private CharacterAsset ca;
// Use this for initialization
void Start()
{
ca = GameObject.Find("Character Asset Manager").GetComponent<CharacterAsset>();
InstantiateCharacterModel();
}
void OnGUI()
{
ChageCharacterMash();
if (GUI.Button(new Rect(Screen.width * .5f - 95, Screen.height * 5f, 30, 30), "<"))
{
model.transform.Rotate(Vector3.up * Time.deltaTime * 100);
}
if (GUI.Button(new Rect(Screen.width * .5f + 65, Screen.height * 5f, 30, 30), ">"))
{
model.transform.Rotate(Vector3.down * Time.deltaTime * 100);
}
}
private void ChageCharacterMash()
{
if (GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height - 35, 120, 30), _charModeName))
{
_charModeIndex++;
InstantiateCharacterModel();
}
}
private void InstantiateCharacterModel()
{
switch (_charModeIndex)
{
case 1: _charModeName = "camaro"; break;
default: _charModeIndex = 0; _charModeName = "x6"; break;
}
if (transform.childCount > 0)
for (int cnt = 0; cnt < transform.childCount; cnt++)
Destroy(transform.GetChild(cnt).gameObject);
GameObject model = Instantiate(ca.characterMesh[_charModeIndex], transform.position, Quaternion.identity) as GameObject;
model.transform.parent = transform;
model.transform.rotation = transform.rotation;
}
private class CharacterAsset : MonoBehaviour
{
public Object[] characterMesh;
}
}
NullReferenceException: Object reference not set to an instance of an object ChangingRoom.InstantiateCharacterModel () (at Assets/script/ChangingRoom.cs:54) ChangingRoom.ChageCharacterMash () (at Assets/script/ChangingRoom.cs:38) ChangingRoom.OnGUI () (at Assets/script/ChangingRoom.cs:22)
This is a page for developers to ask questions about unity that are of interest to at least one other person. It's a wiki of good questions and not a
– Bunny83post-my-code-and-get-syntax-errors-fixed-page. You didn't even take the time to write a real question and you didn't manage to highlicht your code properly. -1+1 for previous comments, -1 for the question.
– half_voxel