Several questions about GameObjects and meshes

Very, very new to unity and java.

http://img594.imageshack.us/img594/5658/17352121.jpg

Here is my code

private var testint = 0 ;
private var testx = 0 ;
private var testy = 0 ;
private var testx_max = 512 ;

function Update(){
	var i2 = 0;
	if (testint < 25000) {
		while (i2 < 512){
			testint += 1;
			i2 += 1;
			testx +=1;
			if (testx >= testx_max){
				testx = 0;
				testy += 1;
			}
			player = new GameObject("Player");
			player.transform.position = Vector3 (testint * 0.25, 5, testy);
			player.AddComponent("MeshCollider");
			player.MeshCollider.sharedMesh("pCube1");
			//------
			player.AddComponent("MeshRenderer");
			player.MeshRenderer.materials.size(1);
			player.MeshRenderer.materials.element0(testd_01);
			//------
			player.AddComponent("MeshFilter");
			player.MeshFilter.mesh(pCube1);
			Debug.Log(testint);
		}
	}
}

1: What am I doing wrong in the code above?

2: Am I missing any extra lines of code - or will the game default on them (scale of 1.0 or 100 for example)

3: “player = new GameObject(“Player”);” What does “Player” do, exactly – as far as I know it’s a name and it ends there.

I’m not asking for someone to write my code, just something to get me in the right direction.


EDIT:
Here is my own code in java, based on Callahan’s code.

GameAssets.js – This gathers all materials and meshes from the inspector for later use in scripts.

var BuildingMaterial : Material[];
var BuildingMesh_Wall : Mesh[];

WallSample.js – This creates a new wall at a random loc every update.

function Update(){
    var SendMesh =  GetComponent(GameAssets).BuildingMesh_Wall[0];
    var SendMat  = GetComponent(GameAssets).BuildingMaterial[Random.Range(0, 6)];
    var SendLoc = Vector3(
                           Random.Range(0.0, 250.0),
                           0.5, //offset wall slightly
                           Random.Range(0.0, 450.0));
    var SendRot = Random.Range(0, 3) * 90; //No need in 45* walls
    CreateSampleWall(SendMesh, SendMat, SendLoc, SendRot);

}

function CreateSampleWall (TheMesh: Mesh ,
                           TheMaterial : Material ,
                           TheLocation : Vector3 ,
                           TheRotation : double ){
                           
                           var WallObject = new GameObject("Wall") ; //Create game object
                           WallObject.transform.position = TheLocation; //Move game object. 
                           WallObject.transform.Rotate(0, TheRotation, 0); //Rotates it.  
                           var MFC : MeshFilter ; //var setup
                           var MRC : MeshRenderer ; //var setup
                           //At this stage we need a mesh filter.  
                           MFC = WallObject.AddComponent(MeshFilter);
                           MFC.mesh = TheMesh;
                           //Now we add the material. 
                           MRC = WallObject.AddComponent(MeshRenderer);
                           MRC.material = TheMaterial;
}

here is his code he sent via PM.

using UnityEngine;
using System.Collections;

public class BuildMeshGameObject : MonoBehaviour
{

	// reference to new object, with mesh/texture
	private GameObject newGO;
	
	// reference to mesh/texture new object will use
	public Mesh theMesh;
	public Texture2D theTexture;
	
	// Use this for initialization
	void Start ()
	{
		BuildNew();
	}
	
	// Update is called once per frame
	void Update ()
	{
	
	}
	
	public void BuildNew()
	{
		if(newGO == null)
		{	
			MeshFilter MFC;
			MeshRenderer MRC;
			
			// create object
			newGO = new GameObject();
			newGO.name = "Created";
			
			// add meshfilter, keeping reference to access it
			MFC = newGO.AddComponent<MeshFilter>();
			MFC.mesh = theMesh;
			
			// add mesh renderer
			MRC = newGO.AddComponent<MeshRenderer>();
			
			// loop thru all materials
			for(int i = 0; i < MRC.materials.Length; i++)
			{
				Debug.Log("material:" + i);
								
				// assign shader
				MRC.materials[i].shader = Shader.Find("Diffuse");
				
				// bit of color, mid green
				MRC.materials[i].SetColor("_Color", new Color(0, 0.5f, 0, 0));
				
				// assign texture
				MRC.materials[i].SetTexture("_MainTex", theTexture);
			}
		}		
	}
}

Player is a name and that is all.

As for the components, there is a difference between the class name of the object and the instance name.

Look at Unity - Scripting API: GameObject

to see shortcuts, and you can also use GetComponent to get particular components on a gameObject.

And to add the MeshFilter you could do:

MeshFilter filter = player.AddComponent(“MeshFilter”);
filter.mesh = pCube1;

You need to get a reference to the component you added or else you need to do GetComponent to retrieve it after it’s created.

The reason for this is that a single GameObject classcould theoretically contain 1000s of MeshFilters if needed. So your filter instances are specifically named and not specified by class name in Unityscript or C#.

I will try to answer the questions as well as some of the compiler problems. If these don’t make sense, please let me know and I will try to elaborate. :slight_smile:

-One thing to note is that AddComponent doesn’t make it a member of the GameObject class. It simply adds the component to it. You can create a component variable of a certain type, or simply of type Component, and use GetComponent() to use it as a variable. For example:

var meshCollider = GetComponent(MeshCollider)

or something to that effect (been coding in C# so my syntax maybe slightly off).

-testd_01 and pCube1 do not exist in the context of that function, so it has no idea what you are referring to.

-GameObject(“name”) will create a basic GameObject with the name “name”. If I’m not mistaken, it is empty with no components.

It will have a Transform component (which is required for all GameObjects), but no others.

–Eric

Thank you ntero, that definitely makes sense to me.

How do I refer to it than - or will they likewise have to be created ingame?

Eric5h5:
Woops. Thanks for clearing that up. GameObjects always get Transform in my mind, so to me outside of that was empty. Would have caused confusion. :slight_smile:

windexglow:
From the code you have, Unity is treating testd_01 and pCube1 as variables passed into a function. Since AddComponent doesn’t actually make them variables, it doesn’t know what those are. You can do

var testd_01 = GetComponent(); //No idea what type this would be
var pCbute1 = GetComponent(MeshCollider);

somewhere before the usage to most likely rid of those errors.

I’m not trying to be dense here, but I’m still confused as to where to go.

			var pCube1 = GetComponent(Mesh);
			var testd_01 = GetComponent(Material);
			var wall = new GameObject("Wall");
			wall.transform.position = Vector3 (testx * 0.25, 5, testy);
			//-----------
			var filter = wall.AddComponent("MeshFilter");
			filter.mesh = pCube1;//null;//GetComponent(pCube1);

This works ingame, so I’m hoping it’s on the right path.

But I’m still confused - what exactly is the first line doing? Simply telling the game “Hey, this mesh is a mesh. Use me with meshes!” or just an empty mesh variable?

To clarify, I’m trying to attach an imported model (pCube1) to a game entity with scripts.

GetComponent() searches within the GameObject the script is attached to for the component of type and returns it. This allows you to grab a component and alter it within script as you see fit.

So the line
var pCube1 = GetComponent(Mesh);
is searching the GameObject the script is attached to for any component of type Mesh and then stores it in the variable pCube1. This allows you to alter the values of the Mesh component or pass it into other functions as needed.

Alright this doesn’t seem to be getting me anywhere, could someone straight out write a script that
A: Attaches a mesh to a gameobject
B: Attaches a material to a gameobject
C: Get said game object to appear correctly ingame

Any particular reason you want to do it all with scripts?

If you were building the mesh from ground up, creating the actual vertexs/polys, it would make sense.

Especially when you could just instantiate -
a)Resource(can load them at runtime)
b)Asset(need a reference in the script/scene)
c)Prefab(with all component/scripts pre-attached)

Unity isn’t supposed to be hard work.

I’m planning on procedurally making buildings ingame - wall section per wall section at a time. I’ve done it several times while modding games (mainly in wc3) so I understand the concepts.

What I’m looking for now is a simple script that attaches a mesh to a gameobject, visible ingame. I have spent hours looking for this on the forum, in tutorials, on the script reference.

I think 99.9% of people would just instantiate it, with those components already on it.

The mesh isn’t so bad, but when you get to the material, you’ll really get your hands dirty.
If the mesh contains multiple materials, shader options, various textures to assign, etc.

I’ll get to that bridge when I get there. But what I want right now is how to attach a mesh to a gameobject via scripts.

Bump. It would be nice to get over this hill…

Just search Mesh in the Unity Docs. It’s right at the top.

As for the Material, just search Material in the Unity Docs. Like I said it’s all right there.

I even checked, It’s right there.[/code]