- Home /
I have no need of this knowledge any more.
Can an Instance Array Recognize Separately Named Instances?
I have a script that makes an array of instances, but I need it to find separate instances. I doubt that they will ever become gameObjects. However, I have a separate question on that if you have a script that spawns gameObjects. Please respond if you do!
Raycasting may suggest a solution, and this question is really planning for the future. However, if you have an answer, please don't wait to post it. I am consistently working on this, and have spent half of my development time on this separate-instance-destroying issue.
Just a recap, I need it to recognize separate instances, perhaps by name, preferably totally separate. I would also love it if one could translate this into Java Script.
Here's the script:
/*
Script to blow up terrain engine trees
(C)2009 by Tom Vogt <tom@lemuria.org>
Released under the Creative Commons "Share Alike" license, see http://creativecommons.org/licenses/by-sa/3.0/
DeadReplace must contain a collider and a rigidbody, or it'll fall through the terrain. :-)
*/
using UnityEngine;
using System.Collections;
public class Tree_Blaster : MonoBehaviour {
public float BlastRange = 30.0f;
public float BlastForce = 30000.0f;
public GameObject DeadReplace;
public GameObject Explosion;
void Explode() {
Instantiate(Explosion, transform.position, Quaternion.identity);
//Get the terrain data of the active terrain
TerrainData terrain = Terrain.activeTerrain.terrainData;
ArrayList instances = new ArrayList();
foreach (TreeInstance tree in terrain.treeInstances) {
float distance = Vector3.Distance(Vector3.Scale(tree.position, terrain.size) + Terrain.activeTerrain.transform.position, transform.position);
if (distance<BlastRange) {
// the tree is in range - destroy it
GameObject dead = Instantiate(DeadReplace, Vector3.Scale(tree.position, terrain.size) + Terrain.activeTerrain.transform.position, Quaternion.identity) as GameObject;
dead.rigidbody.maxAngularVelocity = 1;
dead.rigidbody.AddExplosionForce(BlastForce, transform.position, BlastRange*5, 0.0f);
} else {
// tree is out of range - keep it
instances.Add(tree);
}
}
terrain.treeInstances = (TreeInstance[])instances.ToArray(typeof(TreeInstance));
}
void Update() {
if (Input.GetButtonDown("Fire1")) {
Explode();
}
}
}
Any help is appreciated, provided that you actually know or have thought about it before posting. If you need more information, please comment.
What do you mean by 'recognise'? What is the exact behaviour you are trying to achieve? What do you mean by instance... do you mean any object, a Unity GameObject created using Instantiate etc?
An instance is, for example, a tree painted onto a terrain. It's there in the 3D universe, but not an actual gameObject. They are not instantiated, they are not gameObjects.
Recognition of individual terrain trees is the ability to walk up to one and perform an action that affects it without affecting another.
I exactly want to destroy one tree without destroying any others. This can be done in any way, I don't $$anonymous$$d, but preferably by chopping them (timed destroy).
Follow this Question
Related Questions
NullReferenceException with a text array 1 Answer
trying to instatiate prefab multiple times in array 0 Answers
Find Closest Object by Name 0 Answers
IndexOutOfRangeExeption - Array index is out of range 2 Answers
Storing a variable and it's name 3 Answers