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 /
  • Help Room /
avatar image
0
Question by Hraesvelgr-tc · Aug 23, 2016 at 07:10 PM · c#objectmeshfilter

why is it that my script only works when i drag it on play into the game object?

it previously worked but i had to drag a material into the inspector to set it. i thought it would be neat to just set it in code rather than dragging and dropping. after some research i found a way, but it now will only spawn the cube when i drag the script into the gameobject on play.

meshbuilder.cs

 using UnityEngine;
 using System.Collections;
 [RequireComponent(typeof(MeshRenderer))]
 [RequireComponent(typeof(MeshFilter))]
 public class meshbuilder : MonoBehaviour
 {   
     //GameObject Cube = new GameObject();
     public Material Red = Resources.Load("Red",typeof(Material)) as Material;
     //front face
     public Vector3 vertice0 = new Vector3(-1,1,1); 
     public Vector3 vertice1 = new Vector3(1,1,1); 
     public Vector3 vertice2 = new Vector3(-1,-1,1); 
     public Vector3 vertice3 = new Vector3(1,-1,1); 
     //back face
     public Vector3 vertice4 = new Vector3(1,1,-1); 
     public Vector3 vertice5 = new Vector3(-1,1,-1); 
     public Vector3 vertice6 = new Vector3(1,-1,-1); 
     public Vector3 vertice7 = new Vector3(-1,-1,-1); 
     //left face
     public Vector3 vertice8 = new Vector3(-1,1,-1);
     public Vector3 vertice9 = new Vector3(-1,1,1);
     public Vector3 vertice10 = new Vector3(-1,-1,-1);
     public Vector3 vertice11 = new Vector3(-1,-1,1);
     //right face
     public Vector3 vertice12 = new Vector3(1,1,1);
     public Vector3 vertice13 = new Vector3(1,1,-1);
     public Vector3 vertice14 = new Vector3(1,-1,1);
     public Vector3 vertice15 = new Vector3(1,-1,-1);
     //top face
     public Vector3 vertice16 = new Vector3(-1,1,-1);
     public Vector3 vertice17 = new Vector3(1,1,-1);
     public Vector3 vertice18 = new Vector3(-1,1,1);
     public Vector3 vertice19 = new Vector3(1,1,1);
     //bottom face
     public Vector3 vertice20 = new Vector3(-1,-1,1);
     public Vector3 vertice21 = new Vector3(1,-1,1);
     public Vector3 vertice22 = new Vector3(-1,-1,-1);
     public Vector3 vertice23 = new Vector3(1,-1,-1);
 
     // Use this for initialization
     void Start () 
     {
         MeshFilter Filtration = GetComponent<MeshFilter>();
         Mesh CubeMorph = Filtration.mesh;
         GetComponent<MeshRenderer>().material = Red;
     
         //VerticesArray
         Vector3[] VertArray = new Vector3[]
         {
             //Front  Face
             vertice0,//0
             vertice1,//1
             vertice2,//2
             vertice3,//3
             //back face
             vertice4,//4
             vertice5,//5
             vertice6,//6
             vertice7,//7
             //left face
             vertice8,//8
             vertice9,//9
             vertice10,//10
             vertice11,//11
             //right face
             vertice12,//12
             vertice13,//13
             vertice14,//14
             vertice15,//15
             //top face
             vertice16,//16
             vertice17,//17
             vertice18,//18
             vertice19,//19
             //bottom face
             vertice20,//20
             vertice21,//21
             vertice22,//22
             vertice23//23
         };
 
         //triangles array clockwise points equals visablity
         int[] triArray = new int[]
         {
             //front face
             0,2,3,
             3,1,0,
 
             //back face
             4,6,7,
             7,5,4,
 
             //left face
             8,10,11,
             11,9,8,
 
             //right face
             12,14,15,
             15,13,12,
 
             //top face
             16,18,19,
             19,17,16 ,
 
             //bottom face
             20,22,23,
             23,21,20
         };
 
         //uvs
         Vector2[] uvs = new Vector2[]
         {
             //front face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //back face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //left face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //right face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //top face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0),
             //bottom face
             new Vector2(0,1),
             new Vector2(0,0),
             new Vector2(1,1),
             new Vector2(1,0)
         };
         CubeMorph.Clear();
         CubeMorph.vertices = VertArray;
         CubeMorph.triangles = triArray;
         CubeMorph.uv = uvs;
         CubeMorph.Optimize();
         CubeMorph.RecalculateNormals();
 
     }
 
 
 
     
     // Update is called once per frame
     //void Update () {
     //
     //}
 
 }

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
0
Best Answer

Answer by Hraesvelgr-tc · Aug 24, 2016 at 04:26 AM

i removed this

  public Material Red = Resources.Load("Red",typeof(Material)) as Material;

changing this

 GetComponent<MeshRenderer>().material = Red;

to this

 GetComponent<MeshRenderer>().material = Resources.Load("Red",typeof(Material)) as Material;

and it fixed my problem. Thank you for your reply, I did try it but it did not load the material on the cube. same as my problem was unless i went and dragged it on inspector every play. and at one point the cube was not appearing unless i added it during play. so i created awake function added everything into awake than added awake(); into the start function.

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 Jessespike · Aug 25, 2016 at 05:07 PM 0
Share

That's what i suggested... "Resources load needs to be in a function. Try moving it to Awake or Start"

avatar image
0

Answer by Jessespike · Aug 23, 2016 at 09:34 PM

Do you get an error? The console should be outputting:

 ArgumentException: Load can only be called from the main thread.
 Constructors and field initializers will be executed from the loading thread when loading a scene
 Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

It's referring the material:

 public Material Red = Resources.Load("Red",typeof(Material)) as Material;

Resources load needs to be in a function. Try moving it to Awake or Start:

 public Material Red;
 void Awake () {
     if (Red == null) Red = Resources.Load("Red",typeof(Material)) as Material;
 }
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

225 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 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

C# Find specific object by getting one of its variables 0 Answers

C# Limit touch movement on a raycast 2 Answers

2D - Object follow cursor on X-axis. 0 Answers

When I try this code, the object shoots up into the air. I'm trying to make the camera follow the object. 1 Answer

Destroy moving objcts through touch on screen 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