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 Laminatedweasel · May 24, 2013 at 03:43 AM · c#textureterrain

Adding a texture to terrain through code c#

I've researched this issue through all the other examples and pulled the working code correctly, yet I continue to get the same error. Here is my current code:

 public class RandomTerrainAddTexture : MonoBehaviour {
     private TerrainData tData;
 
 void Start () {
         tData = Terrain.activeTerrain.terrainData;
 
         SplatPrototype[] terrainTexture = new SplatPrototype[1]; 
         terrainTexture[0] = new SplatPrototype(); 
         terrainTexture[0].texture = (Texture2D)Resources.Load("Assets/MyTextures/PossibleRealGrass1");
         tData.splatPrototypes = terrainTexture;  
 }
 }

And here is the error that I absolutely cannot seem to get rid of:

 Terrain splat 0 is null.
 UnityEngine.TerrainData:set_splatPrototypes(SplatPrototype[])
 RandomTerrainAddTexture:Start() (at Assets/MyScripts/RandomTerrainAddTexture.cs:30)

The path is correct and I've tried altering it a ton of different ways and still the same error. Any ideas would be greatly appreciated. Thank you!!

Comment
Add comment · Show 1
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 Owen-Reynolds · May 26, 2013 at 03:59 PM 0
Share

So this is really about how to find an existing texture? Seems like Terrain isn't really the issue.

You could also add public Texture2D someGrass;, drag grass.jpg into the inspector, and use tt[0] = someGrass;. That can make it easier, esp. for non-coders, to Drag around different textures.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by _-_ · May 25, 2013 at 10:48 AM

I've struggled for several hours with the exact same problem yesterday. And......... I've found a solution!

Right now, your texture is in this path ("Assets/MyTextures/PossibleRealGrass1"), right? You need to create a map (EDIT: folder) called resources, and put your texture in there. For example, if you would put your texture in "resources/MyTextures/PossibleRealGrass1", you need to call it as follows:

    terrainTexture[0].texture = (Texture2D)Resources.Load("MyTextures/PossibleRealGrass1");

Because, apparently, Resources/Load will load from the map (EDIT: folder) "resources".

It is a real shame that trying to create a terrain from code is so poorly documented. It's guaranteed to give you some headaches, as I've experienced. I think I've sorted out most of it, so let me know if you have any other questions or need more info on this one (I'm not sure if I've explained it well enough).

EDIT: In my language a folder is called a "map". Probably the fact that terrains are so closely related to maps ( heightmaps, alphamaps, splatmaps and so forth) caused me to make this confusing mistake. So I meant create a FOLDER called resources. My bad, sorry.

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 Laminatedweasel · May 25, 2013 at 05:08 PM 0
Share

Really really appreciate the response this has had me stuck for a while so I've just been working around the currently randomly generated terrain remaining untextured.

Anyhow, not 100% on what you mean create a "map" called resources? I know that when you build your program it generates the folder Resources in the reference folder used to run the EXE, but I'm not quite sure as to how to create a map in that sense I guess. Thanks so much for helping me out!

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

16 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

Related Questions

Distribute terrain in zones 3 Answers

Brushes like in unity built-in terrain. Mouse projection on mesh in edit mode 1 Answer

Multiple Cars not working 1 Answer

Change terrain texture colour 1 Answer

rotate normal map 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