- Home /
Adding material trough a script,
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!
Answer by Pokedlg3 · Dec 31, 2020 at 01:08 PM
You need to create a folder named "Resources" and place the Materials folder inside the Resources folder. Unity only recognizes that you are wanting to access the Resources.Load code if you create a folder, if not it retails Null.
I know I made folder and ist still not working When i drag it trough an inspector while playing a game it works just fine but without me doing it its just purple.
I also mad sub folder Resources/$$anonymous$$aterials and it doesn't work either
I just did some tests and found out what was wrong. Try changing that line:
$$anonymous$$aterial material = Resources.Load<$$anonymous$$aterial>("$$anonymous$$aterials/Dungeon_$$anonymous$$aterial_01_A");
$$anonymous$$aterial materialA = Resources.Load<$$anonymous$$aterial>("$$anonymous$$aterials/Dungeon_$$anonymous$$aterial_01");
For this:
material = Resources.Load<$$anonymous$$aterial>("$$anonymous$$aterials/Dungeon_$$anonymous$$aterial_01_A") as $$anonymous$$aterial;
materialA = Resources.Load<$$anonymous$$aterial>("$$anonymous$$aterials/Dungeon_$$anonymous$$aterial_01") as $$anonymous$$aterial;
The problem is that he was recognizing it with GameObject and needed to convert it to material, with an "as $$anonymous$$aterial" and you had already put the $$anonymous$$aterial variable, so you didn't need to put "$$anonymous$$aterial material =" and only "material ="
Your answer
Follow this Question
Related Questions
Adding materials at runtime or disabling one of multiple materials? 0 Answers
Use material with specific UV 2 Answers
Submeshs doesn't combine with materials 1 Answer
MeshRenderer.materials 1 Answer
Resource.Load always return null 2 Answers