Question by
Yttrandihl · Mar 29, 2017 at 08:31 PM ·
c#scripting problemmeshmeshfilter
Changing a mesh with C# and Resource.Load()
I´m able to remove the actual mesh from my gameobject but the new one is not set :( I tried different ways with MeshFilter and Mesh arrays instead of strings. The path where these models are saved is Assets/Resources/CharacterEditor/Models/myModel
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class ChangeItemsScript : MonoBehaviour {
private Button frisurLeft_button;
private Button frisurRight_button;
private Button augenLeft_button;
private Button augenRight_button;
// private Button frisurLeft_button;
private GameObject frisur;
private int actFrisur = 0;
private GameObject augen;
// CharacterEditor/Frisuren/male_frisur_01_dev.FBX
private string[] frisuren = new string[]
{
"CharacterEditor/Models/male_frisur_01_dev.fbx",
"CharacterEditor/Models/male_frisur_02_dev.fbx"
};
void Start () {
//Get Buttons
frisurLeft_button = GameObject.Find("frisLeft_button").GetComponent<Button>();
frisurRight_button = GameObject.Find("frisRight_button").GetComponent<Button>();
augenLeft_button = GameObject.Find("augenLeft_button").GetComponent<Button>();
augenRight_button = GameObject.Find("augenRight_button").GetComponent<Button>();
//Get GmaeObjects
frisur = GameObject.Find("frisur");
// augen = GameObject.Find("augen");
frisurLeft_button.onClick.AddListener(FrisurLeftClickHandler);
frisurRight_button.onClick.AddListener(FrisurRightClickHandler);
}
void FrisurLeftClickHandler()
{
if (actFrisur == 0)
{
actFrisur = frisuren.Length-1;
frisur.GetComponent<MeshFilter>().mesh = Resources.Load<Mesh>(frisuren[actFrisur]);
} else if (actFrisur == frisuren.Length-1)
{
actFrisur = actFrisur - 1;
frisur.GetComponent<MeshFilter>().mesh = Resources.Load<Mesh>(frisuren[actFrisur]);
}
Debug.Log(actFrisur);
}
void FrisurRightClickHandler()
{
if (actFrisur == frisuren.Length - 1)
{
actFrisur = 0;
frisur.GetComponent<MeshFilter>().mesh = Resources.Load<Mesh>(frisuren[actFrisur]);
}
else if (actFrisur == 0)
{
actFrisur = actFrisur + 1;
frisur.GetComponent<MeshFilter>().mesh = Resources.Load<Mesh>(frisuren[actFrisur]);
}
Debug.Log(actFrisur);
}
}
Comment
Your answer
Follow this Question
Related Questions
Mesh missing in prefab 0 Answers
Add vertex position by it's axis 1 Answer
Why is my mesh not showing outside of an area? 0 Answers
ProBuilder script standalone build exceptions 0 Answers
checking numbers in strings 2 Answers