• 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 /
This question was closed Aug 31, 2014 at 08:00 PM by smirlianos for the following reason:

The question is answered, right answer was accepted

avatar image
Question by smirlianos · Aug 30, 2014 at 11:41 AM · terraingrassautomatic

can't set terrain variable

Hello,

I found this script that generates grass onto the terrain, but I can't set my terrain on the according variable. Any help?

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.IO;
 
 public class GrassCreator : ScriptableWizard  {
 
     public Terrain terrain;  //The variable that I can not assign the terrain
     public int detailIndexToMassPlace;
     public int[] splatTextureIndicesToAffect;
     public int detailCountPerDetailPixel = 0;
         
     [MenuItem ("Terrain/Mass Grass Placement")]
     
     static void createWizard() {
         
             ScriptableWizard.DisplayWizard("Select terrain to put grass on", typeof (GrassCreator), "Place Grass on Terrain");
         
     }
     
     void OnWizardCreate () {
         
         if (!terrain) {
             Debug.Log("You have not selected a terrain object");
             return;
         }
         
         if (detailIndexToMassPlace >= terrain.terrainData.detailPrototypes.Length) {
             Debug.Log("You have chosen a detail index which is higher than the number of detail prototypes in your detail libary. Indices starts at 0");
             return;
         }
         
         if (splatTextureIndicesToAffect.Length > terrain.terrainData.splatPrototypes.Length) {
             Debug.Log("You have selected more splat textures to paint on, than there are in your libary.");
             return;
         }
         
         for (int i = 0; i < splatTextureIndicesToAffect.Length; i ++) {
             if (splatTextureIndicesToAffect[i] >= terrain.terrainData.splatPrototypes.Length) {
                 Debug.Log("You have chosen a splat texture index which is higher than the number of splat prototypes in your splat libary. Indices starts at 0");
                 return;
             }
         }
         
         if (detailCountPerDetailPixel > 16) {
             Debug.Log("You have selected a non supported amount of details per detail pixel. Range is 0 to 16");
             return;
         }
         
         int alphamapWidth = terrain.terrainData.alphamapWidth;
         int alphamapHeight = terrain.terrainData.alphamapHeight;
         int detailWidth = terrain.terrainData.detailResolution;
         int detailHeight = detailWidth;
         
         float resolutionDiffFactor = (float)alphamapWidth/detailWidth;
         
         
         float[,,] splatmap = terrain.terrainData.GetAlphamaps(0,0,alphamapWidth,alphamapHeight);
         
         
         int[,] newDetailLayer = new int[detailWidth,detailHeight];
         
         //loop through splatTextures
         for (int i = 0; i < splatTextureIndicesToAffect.Length; i++) {
             
             //find where the texture is present
             for (int j = 0; j < detailWidth; j++) {
             
                 for (int k = 0; k < detailHeight; k++) {
                         
                     float alphaValue = splatmap[(int)(resolutionDiffFactor*j),(int)(resolutionDiffFactor*k),splatTextureIndicesToAffect[i]];
                         
                     newDetailLayer[j,k] = (int)Mathf.Round(alphaValue * ((float)detailCountPerDetailPixel)) + newDetailLayer[j,k];
             
                 }
                 
             }
             
         }
         
         terrain.terrainData.SetDetailLayer(0,0,detailIndexToMassPlace,newDetailLayer);    
         
     }
     
     void OnWizardUpdate ()
     {
         helpString = "Ready";
         
     
         
         
     }
     
 }
 

Comment

People who like this

0 Show 9
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 AngryBurritoCoder · Aug 30, 2014 at 12:18 PM 1
Share

theres possibly an error in the code, try debug in (f8) in mono, or see the console in unity if any errors pop up

avatar image smirlianos · Aug 30, 2014 at 01:25 PM 0
Share

I don't think there are errors, yesterday I used it just fine on the same project. And every time in the past it worked fine as well

avatar image AngryBurritoCoder · Aug 30, 2014 at 02:27 PM 1
Share

see if it works on another project, restart unity see if it works

avatar image AngryBurritoCoder · Aug 30, 2014 at 02:28 PM 1
Share

or try assign terrain through code

avatar image smirlianos · Aug 30, 2014 at 03:31 PM 0
Share

It turned out it was my fault. Thanks anyways!

Show more comments

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Terrain grass invisible in editor 1 Answer

Unity Terrain Details does not match neighbors 1 Answer

how to import custom plants and grass into unity 1 Answer

Unity terrain grass extremely weird 0 Answers

Plant grass or tree with an image 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