I’m trying to add a material on my Mesh Renderer through a script.
I’m trying to make that when item (weapon) is on a ground it glows and when it’s in hand it’s normal
I have everything set up my 2 materials in a Resources/Materials folder but in console trough Debug.Log and in inspector it shows null/none
I believe error is in Start() function where i try to set variables material and materialA to respective materials.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponsPickup : MonoBehaviour
{
public Rigidbody rb;
public BoxCollider coll;
public Transform player, gunContainer, fpsCam;
Renderer rend;
public Material material, materialA;
public MeshRenderer meshRenderer;
public float pickUpRange, dropForwardForce, dropUpwardForce;
public bool equipped;
public static bool slotFull;
private void Start()
{
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
Material material = Resources.Load<Material>("Materials/Dungeon_Material_01_A");
Material materialA = Resources.Load<Material>("Materials/Dungeon_Material_01");
pickUpRange = 1;
dropUpwardForce = 1;
dropForwardForce = 3;
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
gunContainer = GameObject.FindGameObjectWithTag("WeaponContainer").GetComponent<Transform>();
fpsCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Transform>();
rb = GameObject.FindGameObjectWithTag("Weapon").GetComponent<Rigidbody>();
coll = GameObject.FindGameObjectWithTag("Weapon").GetComponent<BoxCollider>();
meshRenderer = GameObject.FindGameObjectWithTag("Weapon").GetComponent<MeshRenderer>();
//Setup
if (!equipped)
{
rb.isKinematic = false;
coll.isTrigger = false;
}
if (equipped)
{
rb.isKinematic = true;
coll.isTrigger = true;
slotFull = true;
}
}
private void Update()
{
Debug.Log(material,materialA);
//Check if player is in range and "E" is pressed
Vector3 distanceToPlayer = player.position - transform.position;
if (!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKeyDown(KeyCode.E) && !slotFull) PickUp();
//Drop if equipped and "Q" is pressed
if (equipped && Input.GetKeyDown(KeyCode.Q)) Drop();
}
private void PickUp()
{
GetComponent<Renderer>().material = material;
rb.constraints = RigidbodyConstraints.None;
equipped = true;
slotFull = true;
//Make weapon a child of the camera and move it to default position
transform.SetParent(gunContainer);
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.Euler(Vector3.zero);
transform.localScale = Vector3.one;
//Make Rigidbody kinematic and BoxCollider a trigger
rb.isKinematic = true;
coll.isTrigger = true;
}
private void Drop()
{
GetComponent<Renderer>().material = materialA;
equipped = false;
slotFull = false;
//Set parent to null
transform.SetParent(null);
//Make Rigidbody not kinematic and BoxCollider normal
rb.isKinematic = false;
coll.isTrigger = false;
//Gun carries momentum of player
rb.velocity = player.GetComponent<Rigidbody>().velocity;
//AddForce
rb.AddForce(fpsCam.forward * dropForwardForce, ForceMode.Impulse);
rb.AddForce(fpsCam.up * dropUpwardForce, ForceMode.Impulse);
//Add random rotation
float random = Random.Range(-1f, 1f);
rb.AddTorque(new Vector3(random, random, random) * 10);
}
}
Hope this is enough info, Thanks!