x


can't get texture to show via script

(script edited since initial post to show the script in its entirety)

Hey, I'm new with Unity and I'm having a bit of trouble. Here's what I have

using UnityEngine; using System.Collections;

public class populateGroundScript: MonoBehaviour {

public Texture2D grassTexture;

// Use this for initialization
void Start () {
    // make new components in a loop
    // for each component in the loop assign texture and x, y position
    GameObject cube;
    for (int i = 1; i <= 65; i++){
        cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
       cube.transform.position = new Vector3(i*3, 0, 0);
        cube.renderer.material.mainTexture = grassTexture;
    }
}   

}

I assigned the grass texture via Unity's Inspector interface so that's there. (I realize the math is off for what x,y,z I decide to put in, but I figured I can put those in the right place once I know a bit more about what I'm doing)

So that's basically it, I just can't seem to figure out what's wrong with it as to why the texture won't show on the cubes. (I should clarify that I can actually see the cubes lined up across the screen, they're just purely grey)

(Also, after I started this, I found out that I could've made a prefab and cloned it via script to do this, but I figured I started this way, so I'd like to know why it's not working)

more ▼

asked Jun 28 '11 at 05:41 PM

claudekennilol gravatar image

claudekennilol
146 7 9 12

I edited it to show the whole script.. for some reason I can't get all of it inside a code block..

Jun 28 '11 at 07:44 PM claudekennilol

Redid it on my desktop with the same code, but I had a different texture, it worked this time.. I'll see if I can't figure out how to close this.

Jun 28 '11 at 11:58 PM claudekennilol
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Is that your all code for the instantiation and texture assignment? If it is, you should add the rest of the code for a c++ script:

using UnityEngine;
using System.Collections;

public class grass : MonoBehaviour
{
    public Texture2D grassTexture;

    // Use this for initialization
    void Start () {
        // make new components in a loop
        // for each component in the loop assign texture and x, y position
        GameObject cube;
        for (int i = 1; i <= 65; i++){
            cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.position = new Vector3(i*3, 0, 0);
            cube.renderer.material.mainTexture = grassTexture;
        }
    }
}

I've you do it it works perfectly, at least for me

more ▼

answered Jun 28 '11 at 06:48 PM

Ludiares gravatar image

Ludiares
210 2 3

This looks exactly like what I have, I edited my first post to show my script in its entirety. I even attached a light source to my camera in case I couldn't see textures because of lack of light.

Is there some other property of a cube or something I have to enable before textures are visible on it?

I should also say that I attached my script to a blank GameObject.

Jun 28 '11 at 07:46 PM claudekennilol
(comments are locked)
10|3000 characters needed characters left

Try this:

public Material materialll;


 // Use this for initialization
    void Start () {
        // make new components in a loop
        // for each component in the loop assign texture and x, y position
        GameObject cube;
        for (int i = 1; i <= 65; i++){
            cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.position = new Vector3(i*3, 0, 0);
            cube.renderer.material = materialll;
        }
    }
more ▼

answered Jun 28 '11 at 06:35 PM

Alex 4 gravatar image

Alex 4
19 1 2

I did this and changed my texture to a material and used Unity's Inspector window to attach the texture to the material. Now all the cubes the scripts populates are pink as if they're missing a texture altogether.

It's odd because if I just place a cube on the screen and drag the texture onto it it works just fine when I hit the play button (or when I did this and dragged the material onto it).

Is there a special place the texture needs to be? I just have it in the root of the Project window 'cause I'm more worried about learning/getting it to work than being organized.

Jun 28 '11 at 08:18 PM claudekennilol
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4139
x3316
x353

asked: Jun 28 '11 at 05:41 PM

Seen: 1093 times

Last Updated: Jun 28 '11 at 11:58 PM