Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sean Catheroy · Apr 17, 2010 at 02:24 AM · materialsarraysobject-reference-error

Can't assign materials to objects from an array

Can someone please tell me what is wrong with this code? Depending on how I try to solve the problem, I get rotating list of errors, usually to do with a requirement for an instance of an object to reference a non static value.

The troublesome lines are marked with *

I don't understand why this object can't access it's own local variable.

I must be missing something, so if someone could help me see how I'm getting this wrong, I'd be very greatful.

using System; using System.Collections; using UnityEngine;

public class BuildManager { //debug info private static int count = 0;

 /**** local to class *****/
 *** public Material[] baseMaterials;***

 public static void buildRoad(int x, int z){
     Debug.Log("I have built "+BuildManager.count+" road tiles so far");
     Tile tile = new Tile();
             tile.Awake(x, z, _ROAD);
     tile.GO = GameObject.CreatePrimitive(PrimitiveType.Plane);
     tile.GO.name = "road_"+x+"_"+z;
     tile.GO.tag = "road";       
     tile.GO.transform.position = new Vector3(x, 0, z);
     ***tile.GO.transform.renderer.material = baseMaterials[0];***
     }

I have tried creating a Material manager class and assigning from there, I have tried deriving this class from MonoBehaviour... something just isn't clicking.

Any and all help is welcome.

Best Wishes,

S.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Molix · Apr 17, 2010 at 04:07 AM

The "buildRoad" function is static, but the "baseMaterials" array is not--it is an instance member. For comparison, you have referenced "BuildManager.count" within the same function as a static member. Declaring baseMaterials as static would get it compiling (but it still may not accomplish what you're trying to do, which is not entirely clear).

Comment
Add comment · 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
0

Answer by Sean Catheroy · Apr 17, 2010 at 02:48 PM

Thanks for your reply, Molix :)

I tried to add this reply as a comment, but it wouldn't take...

Basically, the functionality I'd like to end up with is this: 1) Have one single instance of the BuildManager running, like a daemon. Any time a road needs to be built, the BuildManager should get called to put the tile down in the location specified. While creating that Tile, it should assign an appropriate material to the tile from the array of materials I assign to it. I've created those materials in the Editor, and am assigning them through the editor as well.

2) A single instance of a game state manager, which takes care of dispatching and receiving requests and coordinating game logic, calls BuildManager any time it needs a Tile built. It sends the coordinates and type of building to be built to the buildmanager. The buildmanager might either return the instantiated building for the manager daemon to keep track of, or might add it to a master reference list (I currently do this).

I had this all working in Javascript, but needed an easy way to create and manipulate 2d arrays of unspecified sizes.

I understand what you're saying with respect to conflict between a static function, and the non-static array... I looked that up and discovered that issue in c#. But, if I declare baseMaterials as static then it doesn't get exposed to the editor... I could assign materials in code, but for heavens sake, what a pain to have to write that code as well.

Any thoughts?

Comment
Add comment · 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
0

Answer by Tetrad · Jul 10, 2010 at 06:40 PM

On the other end of the spectrum of what Molix is saying, you could make BuildManager a singleton.

So something like this

// create a game object and assign this to it class BuildManager : Monobehaviour { public Material[] baseMaterials; // assign this in editor

 // very simple singleton pattern (doesn't prevent multiple instances, though)
 static BuildManager instance;
 void Awake() { instance = this; }

 // your static function
 public static void BuildRoads( /* whatever */ )
 {
     // your current code

     // get the instance's baseMaterials array and use that
     tile.GO.transform.renderer.material = instance.baseMaterials[0]; 

     // etc

 }

}

Alternatively you could make BuildRoads non static, make instance a public variable, remove the instance from the line of code above, and then do BuildManager.instance.BuildRoads

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

hey guys i'm trying to switch materials back while in arrays (multiple objects/multiple materials) 1 Answer

How do you change the tint of an entire material array for differing objects? 1 Answer

How could I collect all gameobject Materials? 1 Answer

Changing the material of all children, then change back to old one 1 Answer

Getting an error trying to assign a value to an array of classes element 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