Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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 Brownbeat77 · Jul 17, 2015 at 04:00 AM · c#texturematerialobject referencenon-static

ALWAYS GET CS0120: Changing material of an object in a script

If my terminology or anything is off I apologise and hope you know what I'm talking about:

No matter what I try I just cannot get this to work despite doing exactly what Im told in many tutorials

I simply want to change the material of an object to a certain material based on a variable i have set up, but I always get the error message: CS0120: An object reference is required to access non-static member.

This is one of my simpler attempts:

 public Texture WarriorBD;
 public Texture MageBD;
 public Texture RogueBD;

 private void SetBackdrop(int jobSelection){
     if (jobSelection == 0) {
         Renderer.material.SetTexture("CCBackdrop", WarriorBD);
     }else if(jobSelection == 1) {
         Renderer.material.SetTexture("CCBackdrop", MageBD);
     }else if(jobSelection == 2) {
         Renderer.material.SetTexture("CCBackdrop", RogueBD);
     }
 }

I then call 'SetBackdrop' in another script, which has worked with methods with the same structure as this.

The variable 'jobSelection' isnt an issue as it works in these other methods.

"CCBackdrop" is the name of the object I wish to change the material for.

The public textures sometimes pop up in the inspector panel and I can assign textures to them, but sometimes they don't turn up even when the script is identically written (but I don't think it is related to this problem)

Please tell me what I'm doing wrong and/or how I might do want I'm trying to do.

Comment
Add comment · Show 2
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 meat5000 ♦ · Jul 16, 2015 at 11:58 PM 0
Share

Which renderer are you referencing?

avatar image Brownbeat77 · Jul 18, 2015 at 07:19 PM 0
Share

for the object I will attach the script to. Is this not how to do it?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Positive7 · Jul 17, 2015 at 09:08 PM

 public Texture WarriorBD;
 public Texture MageBD;
 public Texture RogueBD;
 
 private void SetBackdrop(int jobSelection){
     if (jobSelection == 0) {
         GetComponent <Renderer>().material.SetTexture("CCBackdrop", WarriorBD);
     }else if(jobSelection == 1) {
             GetComponent <Renderer>().material.SetTexture("CCBackdrop", MageBD);
     }else if(jobSelection == 2) {
             GetComponent <Renderer>().material.SetTexture("CCBackdrop", RogueBD);
     }
 }
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 Brownbeat77 · Jul 18, 2015 at 07:16 PM 0
Share

Thank you Positive7, but with this code I get the error

CS0103: The name `GetComponent' does not exist in the current context for lines 7, 9 and 11

Am I missing a namespace or something?

avatar image tanoshimi · Jul 18, 2015 at 07:28 PM 0
Share

Yes. Your entire code needs to be wrapped in a class, and you need to use the UnityEngine namespace. These should have already been included in your code template when you created a new C# file from the Unity editor though...

avatar image Brownbeat77 · Jul 19, 2015 at 02:07 AM 0
Share

I found why GetComponent wasnt working:

using UnityEngine; using System.Collections;

public class CharacterCreationFunctions {

I wasn't inheriting from monobehavior. Thats what it needed

avatar image Positive7 · Jul 19, 2015 at 11:50 AM 0
Share

Yeah, Sorry I thought that was obvious :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Drawing a display panel with textures. 1 Answer

Light based texture change on material 0 Answers

Multiple Cars not working 1 Answer

Howto save ProceduralTexture as PNG 2 Answers

Help with understanding UV's in script created mesh 0 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