- Home /
how to hide the mesh of the gameobject ?
Hai to all....i want to change the mesh of the game object....now i got small idea about mesh swapping....theese are my codings..
public class meshchanging
{
public Gameobject initialobject;
public Gameobject swapobject;
msh initialmesh;
mesh swapmesh;
Gameobject theTarget;
void start(){
theTarget=initialObject;
initialmesh=initialobject.getcomponent<meshfilter>().mesh;
swapmsh=swapobject.getcomponent<meshfilter>().mesh;
}
void update(){
theTarget.getcomponenet<meshfilter>().mesh=swapmesh;
}
}
now whats my problem was i cannot hide my initial game object......what my objective is when the new mesh is shown the old one want to hide.....help me in scripting...... and give some another coding to change the mesh while the game running ...without mesh swapping.......Thank in advance...
Just have both mesh objects on your object from the start. Then turn off the mesh render for the one you don't want and turn it on for the one that you do. See this part of the docs for the script :
http://docs.unity3d.com/ScriptReference/Renderer-enabled.html
Answer by CHPedersen · Nov 10, 2014 at 03:28 PM
You've actually kind of almost got it, but there are several things that seem strange here. The strange part is that your script actually has overall correct structure, but the errors indicate there are some very basic things about programming you would benefit from studying.
First of all: Spelling. C# is not a case insensitive language, and it has zero tolerance for spelling mistakes. Correct spelling is of paramount importance! You must not spell variables and method names in lower case.
I took the liberty to correct the naming mistakes and also apply normal C# naming conventions. The script then looks like this:
using UnityEngine;
using System.Collections;
public class MeshChanging : MonoBehaviour
{
public GameObject InitialObject;
public GameObject SwapObject;
private Mesh initialmesh;
private Mesh swapmesh;
public GameObject theTarget;
void Start()
{
theTarget = InitialObject;
initialmesh = InitialObject.GetComponent<MeshFilter>().mesh;
swapmesh = SwapObject.GetComponent<MeshFilter>().mesh;
}
void Update()
{
theTarget.GetComponent<MeshFilter>().mesh = swapmesh;
}
}
Second of all: Scripts must derive from MonoBehaviour. Your own classes (classes that do not derive from MonoBehaviour) cannot be attached to GameObjects, and therefore do not get their Update and Start methods called. I added the required inheritance to the class declaration.
Third of all: Consider carefully what your script actually does, then think about whether that is what you want. Remember that Update gets called every single frame. Do you want to immediately swap the mesh on the first frame, and then eternally set it to the same mesh every frame thereafter? My guess is: probably not. ;)
This is enough answer to your question for now. Try to edit it further, play with what it does, then ask another question when or if you find it doesn't quite do what you imagined.
Best of luck!
Your answer
Follow this Question
Related Questions
Converting a number of runtime-created prefab GameObject cube's for export 0 Answers
How to assign Icosphere to meshFilter with script? 1 Answer
Mesh dissapearing from Resources 0 Answers
Using MeshFilter.mesh with static ground plane makes object turn into a plane. 0 Answers
NullReferenceException on assigning vector3 to mesh.vertices - no effect on assignment? 1 Answer