Sorry… ill try to be more accurate…
Im doing a tower defense game, and i have a interactive GUI which let me drag the “towers” from the GUI to the scene. The code that manages that is:
using UnityEngine;
using System.Collections;
public class Gui : MonoBehaviour {
public Transform clone;
public Transform ObjectToPlace;
private GameObject obj;
private Vector3 screenPoint;
private Vector3 offset;
private Vector3 mousePos;
private Ray ray2;
private RaycastHit hit2;
int layerMask2;
public bool dest=false;
private Rect boton1=new Rect(10, 40, 50, 80);
private Rect boton2=new Rect(70, 40, 50, 80);
void OnGUI() {
GUI.BeginGroup(new Rect(0, Screen.height-100, Screen.width, 100));
GUI.Box(new Rect(0, 0, Screen.width, 100),"");
CheckButton(boton1,1);
CheckButton(boton2,2);
GUI.Button(boton1,"");
GUI.Button(boton2,"");
GUI.EndGroup();
}
void CheckButton(Rect boton,int cod) {
switch(cod) {
case 1:obj=(GameObject) Resources.Load("Cube01");break;
case 2:obj=(GameObject) Resources.Load("Cube02");break;
}
ObjectToPlace=obj.transform;
if(boton.Contains(Event.current.mousePosition)) {
if(Input.GetButtonDown("Fire1")) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
var layerMask = 1 << 8;
if (Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask)) {
clone = hit.transform;
}else{
layerMask = ~layerMask;
if (Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask)) {
clone = (Transform) Instantiate(ObjectToPlace, hit.point, Quaternion.identity);
}
}
}
}
if(Input.GetButtonUp("Fire1")) {
if ((!dest)&&(Physics.Raycast(ray2, out hit2, Mathf.Infinity, layerMask2))) {
Destroy(clone.gameObject);
dest=true;
}
clone=null; // clear clone variable
}
if(Input.GetButton("Fire1")) {
if (clone!=null) {
ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
layerMask2 = 1 << 8;
layerMask2 = ~layerMask2;
if (Physics.Raycast (ray2, out hit2, Mathf.Infinity, layerMask2)) {
clone.position = hit2.point;
}
layerMask2 = 1 << 10;
}
}
}
}
dont pay attention to the GetButtonUp() function, the destroy was just for testing.
the problem comes now…
my cubes have this script:
using UnityEngine;
using System.Collections;
public class Build : MonoBehaviour {
private Material textureColor;
public Material greenColor;
void Start() {
textureColor=renderer.material;
greenColor=new Material (Shader.Find("greenTrans"));
}
void OnTriggerStay(Collider other) {
if(other.gameObject.tag==Tags.validSpot) {
renderer.material = greenColor;
}
}
void OnTriggerExit(Collider other) {
if(other.gameObject.tag==Tags.validSpot) {
renderer.material=textureColor;
}
}
}
The Cubes have a red material, and i have another material with Transparency/difusse named “greenTrans” but i get an error in runtime:
NullReferenceException
UnityEngine.Material…ctor (UnityEngine.Shader shader) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/Graphics.cs:1584)
Build.Start () (at Assets/Scripts/Build.cs:13)
I know… i ain’t getting the Material from my Assets.
The box needs to collide with a Mesh collider but seems it isnt colliding with it.
Sorry about my english… i’m trying to be as accurate as i can :(.