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 chinapdyh · Aug 18, 2014 at 03:08 PM · terrainmaterial

Dynamic create Terrain and change the material by Script

I want to create Terrain by the script,and I used the terrainData to Create a terrain.Now I want to give the terrain a material.But I have some questions: when I write the code like: ter.renderer.material = mat;

the Unity will give this exception:

===================Exception

MissingComponentException: There is no 'Renderer' attached to the "Terrain" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "Terrain". Or your script needs to check if the component is attached before using it.

=================

but when I add Renderer like this: ter.AddComponent("Renderer");

the Unity will give a Warning like this

=================Warning

Cannot add component of type 'Renderer' because it is abstract. Add component of type that is derived from 'Renderer' instead.

=================

So I search this problem on Google.Some person said that I should use the MeshRenderer instead.So I Write the code like : ter.AddComponent("MeshRenderer");

but it also doesn't work:

========================Warning

Can't add component 'MeshRenderer' to Terrain because such a component is already added to the game object!

========================

Now I do not know how to do, who can tell me. Thanks very much.I want to create the terrain dynamic and create the material dynamic by 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by _dns_ · Aug 18, 2014 at 03:40 PM

Hi,

Try setting the material with Terrain.materialTemplate. I can't see this in the documentation but it works on my project (in some Editor code, didn't try it at run-time)

Comment
Add comment · Show 3 · 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 chinapdyh · Aug 18, 2014 at 03:51 PM 0
Share

Thanks,do you have some sample code?I can't find the Terrain.materialTemplate.Is that an API of terrain?

avatar image chinapdyh · Aug 19, 2014 at 04:01 AM 0
Share

Thanks a lot,I have solved this problem by your way.I find the terrain.materialTemplate . At the beginning I create the Terrain by CreateTerrainGameObject, it returns a Gameobject, so I can not find this interface. Now I find it in a Terrain variable 。

         Terrain t = ter.GetComponent<Terrain>();
         if(t)
         {
             t.materialTemplate = mat;
         }
avatar image _dns_ · Aug 21, 2014 at 05:31 PM 0
Share

Cool, glad it helped! (wasn't notified of your previous comment until now, sometimes notification lags a few days...)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Applying offset to a texture via script 1 Answer

ExportTerrain doesn't work 1 Answer

Procedurally generating terrain? 0 Answers

Terrain material breaking when adding holes 0 Answers

Modelling Terrain in Blender 1 Answer


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