Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vexxtv123 · Dec 31, 2020 at 12:36 PM · materialsmeshrenderer

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexxtv123 · Dec 31, 2020 at 01:34 PM 0
Share

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.

avatar image vexxtv123 · Dec 31, 2020 at 01:35 PM 0
Share

I also mad sub folder Resources/$$anonymous$$aterials and it doesn't work either

avatar image Pokedlg3 vexxtv123 · Dec 31, 2020 at 01:50 PM 2
Share

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 ="

avatar image vexxtv123 Pokedlg3 · Dec 31, 2020 at 05:08 PM 0
Share

Yep,that works thaks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

113 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges