#WebGL material change doesn't work on build
1 messages · Page 1 of 1 (latest)
I'm fairly new also but am working on a webgl game too so maybe I've hit something similar.
Should I test out if I can change the material as a gameobject instead of a prefab?
In the actual public variable for the script I define it's prefab counterpart
http://gyanendushekhar.com/2018/09/16/change-material-and-its-properties-at-runtime-unity-tutorial/ have you looked at this
I have not, I'll give it a shot
I don't think you can set the material just by changing the name
SphereMaterial = Resources.Load<Material>("SphereMaterial");
``` I think this is the key line
like I said, it works fine in the editor, the material changes
I see
ive got to load it
ill give it a shot
yeah let me know how it works. I know in a webgl build you don't have access to everything as easily.
so for instance if you are going to load anything it has to be in the resources director or below
the assest folder right?
assets*
like say if the material were in assets > Mat > SkinRed id write SkinRed = Resources.Load("Mat/SkinRed") as Material;
Yes Assets/Resouces but when you use Resources.Load it uses Asset/Resources as the root
I think so. It would have been a long time if I had to do it myself (or maybe even some asset I use did it.
also mine is capital R Resources and might be case sensitive
Are you able to explain how it gets the right meshRenderer in this line? MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
is it the meshrenderer of the material
or the player/gameobject
GetComponent<T> has an implicit gameObject in front of it
so it looks on the game object that the script is attached to
oo so the gameobject is in a different scene so GameObject.Find("Player"); will be of use
Potentially, are you aware that scenes are garbage collected when you load a new one?
What does that mean 😅
so garbage collection is part of memory management. Which is done automatically in unity. What garbage collection does is it deletes things that no longer have any references or are out of scope.
oooo so that wouldn’t work would it
so in a unity game if you load a scene, everything in the old scene is destroyed
there are some things you can do (don't destroy on load) but in general you can't reference things in other scenes
i’ll try the don’t destroy on load
is there a way to make a function get called everytime a scene is switched to?
Most people create something called a scene manager
that will be a static class that manages your scene transitions
i see
the benefit is if you want to call a function after each scene switch. You put it in the manager load scene function and it just happens every time
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//this class manages the transitions between scenes
public static class SceneController
{
//add new scenes to this enum
public enum Scene{
LoginScene,
MainMenuScene,
ComputerBattleScene,
}
public static void LoadScene(Scene scene){
SceneManager.LoadScene(scene.ToString());
}
}
``` here is a really old and probably bad one I had but you get the idea