#WebGL material change doesn't work on build

1 messages · Page 1 of 1 (latest)

leaden stirrup
#

I'm fairly new also but am working on a webgl game too so maybe I've hit something similar.

vapid swift
#

Should I test out if I can change the material as a gameobject instead of a prefab?

leaden stirrup
#

it looks like PlayerPrefab is already a game object

#

its not an actual prefab

vapid swift
#

In the actual public variable for the script I define it's prefab counterpart

leaden stirrup
vapid swift
#

I have not, I'll give it a shot

leaden stirrup
#

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
vapid swift
#

like I said, it works fine in the editor, the material changes

#

I see

#

ive got to load it

#

ill give it a shot

leaden stirrup
#

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

vapid swift
#

assets*

#

like say if the material were in assets > Mat > SkinRed id write SkinRed = Resources.Load("Mat/SkinRed") as Material;

leaden stirrup
#

Yes Assets/Resouces but when you use Resources.Load it uses Asset/Resources as the root

vapid swift
#

Do I have to create a resources folder?

#

i dont have one by default

leaden stirrup
#

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

vapid swift
#

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

leaden stirrup
#

GetComponent<T> has an implicit gameObject in front of it

#

so it looks on the game object that the script is attached to

vapid swift
#

oo so the gameobject is in a different scene so GameObject.Find("Player"); will be of use

leaden stirrup
#

Potentially, are you aware that scenes are garbage collected when you load a new one?

vapid swift
#

What does that mean 😅

leaden stirrup
#

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.

vapid swift
#

oooo so that wouldn’t work would it

leaden stirrup
#

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

vapid swift
#

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?

leaden stirrup
#

Most people create something called a scene manager

#

that will be a static class that manages your scene transitions

vapid swift
#

i see

leaden stirrup
#

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
vapid swift
#

okay, sounds like a lot of refactoring

#

thanks for the help