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…
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.
Fiddling with the mesh filter is a good way to cause memory hitches. Doing so causes the engine to unload your mesh data from the GPU and upload new mesh data. Unless your app is memory bound, you probably want to just have both mesh data available on the GPU during the life time of your scene.
You want a hierarchy like this:
Parent Object
|- Game Object A
|- Game Object B
And Game Objects A & B will have components like this:
Parent Object
- Mesh Swapper
Game Object A
- Mesh Filter
- Mesh Renderer
Game Object B
- Mesh Filter
- Mesh Renderer
Keep everything active EXCEPT the Mesh Renderer on the game object you want to hide.
When you’re ready to “swap meshes”, simply set one Mesh Renderer to inactive and the other to active.
Your code on in MeshSwapper might look like this
using UnityEngine;
public class MeshSwapper : MonoBehaviour {
[SerializeField] private MeshRenderer _meshRendererA;
[SerializeField] private MeshRenderer _meshRendererB;
public void SwapMeshes() {
_meshRendererA.enabled = !_meshRendererA.enabled;
_meshRendererB.enabled = !_meshRendererB.enabled;
}
}
You’ll of course want to do your due diligence there and add an Awake to verify state and use some other input handler to call SwapMeshes().