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);
}
}
}
}