- Home /
Build problem, missing variables
(Sorry for my english) I have a scene with 3 mesches. Each mesh has this script:
using UnityEngine;
using System.Collections;
public class Button : MonoBehaviour {
public Shader shader1;
public Shader shader2;
public Object levelToLoad;
void Start()
{
shader1 = Shader.Find("Diffuse");
shader2 = Shader.Find("Self-Illumin/Diffuse");
renderer.material.shader = shader1; //to include the shader Self-Illumin/Diffuse in the build
}
void Update()
{
}
void OnMouseOver()
{
renderer.material.shader = shader2;
}
void OnMouseExit()
{
renderer.material.shader = shader1;
}
void OnMouseDown()
{
Application.LoadLevel(levelToLoad.name);
}
}
I assign the script to each mesh and the scene to load to each script, but when i build and run the progect and i click on a mesh instead of load the assigned scene appear an error on the development console.
This is the error: NullReferenceException: Object reference not set to an istance of an object
How can i fix this error?
It depends on where that Unassigned Object is in code, try to click on that error in Console and it should take you to the line where you're trying to access that unassigned object and look to Inspector, whether is that object asigned there or from another script.
Post the full error including line numbers.
Good attempt at formatting but you pressed the BlockQuote ins$$anonymous$$d of Format (101010) button.
I think this question might be answered here :
Probably, your Shader.Find cannot find the mentioned shaders. Did you check the link I posted.
Answer by dsada · Jul 30, 2014 at 01:49 PM
In a few words the problem is that the shaders dont get compiled in the build.
When Unity creates your build it compiles everything in the Resources folder but from the Assets folder only those objects are compiled that you have a reference in the compiled scenes or in the Resources folder.
For example if you have a material in the Resources folder that uses the "Self-Illumin/Diffuse" shader, this shader also compiles even if it is not in the Resources folder. Also if you put a scene in the build that has a gameobject that uses a material from the Assets folder and that material uses this shader the shader will be in your build.
But i guess you dont have a reference to the shader and it is not in the Resources folder. It is normal that it is working in the editor because all the resources are available in the editor.
The solution might be that you create materials in the Resources folder and use the needed shaders on them. Or just put the shaders in the folder but honestly I dont know where to find them. Also if you created the materials you dont need to use Shader.Find() then you can just use (Resources.Load("materialName") as Material) and you can assign them to the meshes.
One other note: If your meshes dont use the same material dont use renderer.material because it makes a copy of it. Use renderer.sharedMaterial instead.
The shaders work fine because the mesh "PLAY" has the shader self-Illu$$anonymous$$/Diffuse, the other meshes have the shader diffuse, qhen i play the game renderer.material.shader = shader1
set the shader to diffuse because it is set to self-illu$$anonymous$$/diffuse.
The wrong part is that loadToLevel is assigned in the ispector but, it isn't assigned in the build. I'm working to a solution...
Oh i see. Is it important that loadToLevel is an Object and not a string?
Your answer
Follow this Question
Related Questions
Error when build the game 1 Answer
A node in a childnode? 1 Answer
Build Problem - asset is marked as don't save 9 Answers
Distribute terrain in zones 3 Answers
Built game error 0 Answers