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
1
Question by DEtH_MoroZ · Feb 02, 2011 at 11:53 PM · materialsstatic-variableschoose

Static variable and Materials

Hi there! im trying to create smt like lego game. Now im trying to create system, thats allow player to choose between several (actually 66) materials for blocks. here is the code: (java)

var boxIconTextures : Texture[]; var materialsRepresentation : Material[];

static var MaterialOfElement: Material;

function MaterialOfElementChange() {

var materialSelectionGridInt : int = 0;

materialSelectionGridInt = GUI.SelectionGrid (Rect (25, 80, 700, 480), materialSelectionGridInt, boxIconTextures, 16); switch (materialSelectionGridInt) { case (materialSelectionGridInt): materialsRepresentation[materialSelectionGridInt] = MaterialOfElement; print ("material switched"); break; } }

and here is code for element creating:

var BoxTexture; var check ; var thePosition ; var Brick : LayerMask; static var MaterialOfElement;

function Fire () {

thePosition = transform.TransformPoint(Vector3.forward * 1.5);

check = Physics.CheckSphere (thePosition, 0.3, Brick);

if (check == true) return; if (check == false)

var element = GameObject.CreatePrimitive(PrimitiveType.Cube); element.renderer.material.mainTexture = BoxTexture; element.layer = 8; element.renderer.material = CubeGui.MaterialOfElement; element.gameObject.AddComponent ("Destroy"); element.transform.position = thePosition; element.gameObject.AddComponent ("SetToGrid"); }

the problem is: im choosing new material from MaterialOfElementChange() but its wont apply on creating objects. i manage that its happen cos of mistakes in MaterialOfElementChange() but everything looks like its work correctly. may be its happen cos static vars not allow to use it for materials? :D (cant believe that)

edit1: ive tryed what Simple says, but it wont work. i find out that when i start script of material change (ongui part) its says "material changed" lol so i gonna check some docs on grids and toolbars.

edit2: forget to say:

materialsRepresentation = new int[10];

equals

Cannot convert 'int[]' to 'UnityEngine.Material[]'.

array is created via inspector, so, somehow grid just dont want to work. maybe its happen cos of another toolbar in same script?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Simple · Feb 03, 2011 at 01:33 AM

switch (materialSelectionGridInt) { case (materialSelectionGridInt): materialsRepresentation[materialSelectionGridInt] = MaterialOfElement; print ("material switched"); break;

I suppose your switch-case doesnt work. It should be like:

switch (materialSelectionGridInt) {
//if (materialSelectionGridInt==1)
        case 1: materialsRepresentation[1] = Material1; 
print ("material switched"); break;
//if (materialSelectionGridInt==2)
        case 2: materialsRepresentation[2] = Material2; 
print ("material switched"); break;
//etc
//...
}

UPDATE: And I am not sure byt you have created empty and not editable array "materialsRepresentation" because you use BuiltIn array. And you need to add this

materialsRepresentation = new int[10];

Because you cant resize BuiltIn arrays without reinitializing. http://unity3d.com/support/documentation/ScriptReference/Array.html

Comment
Add comment · Show 1 · 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 DEtH_MoroZ · Feb 03, 2011 at 03:39 AM 0
Share

array is flooded via inspector, everything is ok. and it will be not changed in gameplay (there are 66 colors + shaders, cant beleve anyone need more lol).

on another hande, if ill make every case it will be rly big bunch of useless code, thats y i manage do it like that:

materialSelectionGridInt sending int, i get it to use as a number of "case" and number of choosed material

materialsRepresentation[materialSelectionGridInt] = $$anonymous$$aterialOfElement

but, anyway, tyvm ill try to do it

avatar image
0
Best Answer

Answer by DEtH_MoroZ · Feb 08, 2011 at 03:24 PM

Problem was here:

materialsRepresentation[materialSelectionGridInt] = MaterialOfElement;

must be:

MaterialOfElement = materialsRepresentation[materialSelectionGridInt];

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

How to define surface properties on a 2d sprite? 1 Answer

EXC_BAD_ACCESS from Unity::Material::CreateMaterial When running simple app on iPhone. 3 Answers

Replacing sharedMaterials by an existing Material C# 1 Answer

modifying renderer.materials resets scaling of renderer.bounds until transform.position is updated 0 Answers

Tool for blending maps for Unity materials 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