Hi, We are trying to make an Avatar windows configuration, to choose between diferent meshes and materials for each of the 3 parts of the model and later pass this model to a different scene where you can use your avatar.
The problem is that we have reach to mantain the selected meshes but the materials get lost during the scene change.
We are using a prefab as an intermediate object to keep the mesh and material.
The code:
...
//parts of the prefab model (head, body and legs)
var avatarObject1: GameObject; //
var avatarObject2: GameObject; //
var avatarObject3s: GameObject; //
//parts of the changing model (head, body and legs)
var object1: GameObject; //
var object2: GameObject; //
var object3: GameObject; //
//Reference to the prefab
var DontDestroyPrefab: GameObject;
...
function Awake()
{
DontDestroyOnLoad(DontDestroyPrefab);
}
onGUI()(
...
//if PLAY pressed:
//This works good for the mesh
skinnedMesh=avatarObject1.GetComponent(SkinnedMeshRenderer);
skinnedMesh.sharedMesh = object1.GetComponent(SkinnedMeshRenderer).sharedMesh;
//this doesn´t keep the persistance between scenes
objectRenderer=avatarObject1.GetComponent(SkinnedMeshRenderer);
objectRenderer.sharedMAterial=object1.GetComponent(SkinnedMeshRenderer).sharedMaterial;
//the same for the other parts (2 and 3) of the model
...
}
If anyone knows a better way to pass a model (mesh and materials) between scenes please let us know.
Thanks =)
And you’re assigning that in the inspector I then imagine?
I would just do it this way:
DontDestroyOnLoad(this);
Just put that at the top of your .js. Make it your first line, even before Awake().
This will prevent the object which this script is attached to from being destroyed.
Ok thanks, I have just tested it but doesn´t work neither…
The problem is that I can keep the different mesh choosen but not the materials… and I don´t know why… or if there is a better way to do that.
Any idea?
i have the same problem too, and i face this problem in the past …
the material is an instance from a real material from the project panel so that it has been missing when trying to keep it from scene to another , i don’t know what is the solution in this case but there must be one…
the solution that am working on it now is saving all the material on another script and then assign them again when the new scene begin, this is the only solution i guess… but if anyone has another idea plz share it with us … thanx alot
I have an instantiated object that’s set DontDestroyOnLoad. It works fine when loading a new scene, but when reloading the same scene (there’s a loading scene in between in both cases) the object’s material goes missing.
I suppose I could solve this by setting DDOL on the material, but what I don’t get is why it works when loading a different level and not when reloading the same one? This happens on all levels, so if I load Level2 from Level1, the material stays, but not when I load Level2 from Level2.