Hi,
First, I’m really sorry, I don’t speak english very well, and I’m learning C# by myself so it’s a little bit hard for me.
What my script do is to instantiate a new Gameobject at the center of each poly of an existing gameobject (the one which contains my script). It works fine, but when I try to name my new gameobject I get a NullReferenceException.
Here is my script, I hope he is easy to read :
using UnityEngine;
using System.Collections;
public class FireInitialize : MonoBehaviour {
//the prefab I want to be instantiated
public GameObject FireSensor;
GameObject FireSensors;
// Use this for initialization
void Start () {
Mesh goMesh = GetComponent<MeshFilter> ().mesh;
//explore all polygons of my mesh
for(int i=0; (i+2)<goMesh.triangles.Length; i+=3){
//Find the center of the polygon i'm working on (bar is for barycentre, french name for centroid)
Vector3 Bar1= goMesh.vertices[goMesh.triangles*];*
-
Vector3 Bar2= goMesh.vertices[goMesh.triangles[i+1]];*
-
Vector3 Bar3= goMesh.vertices[goMesh.triangles[i+2]];*
-
Vector3 Bar = new Vector3((Bar1.x+Bar2.x+Bar3.x)/3,*
-
(Bar1.y+Bar2.y+Bar3.y)/3,*
-
(Bar1.z+Bar2.z+Bar3.z)/3);*
-
//Instantiate my object in the center of the poly, and assign to his name a new number to differentiate it to the other one (will be used after to interact with my polys)*
-
string SensorNumber = i.ToString();*
-
FireSensors = Instantiate(FireSensor.transform, Bar, transform.rotation) as GameObject;*
-
FireSensors.name="FireSensor"+SensorNumber;*
-
}*
-
}*
}
Thanks a lot for your help