- Home /
accessing variables from nearby gameobjects (Prim's Algorithm)
Hey everyone. I'm trying to make a labyrinth with a randomly generated map
like this one: http://www.youtube.com/watch?v=ucWX34Vrel8
I currently have given each point a randomly generated number, but how can I check to see if the number of adjacent points are larger or smaller?
This is what I currently have:
var number : float;
function Start() {
number = Random.Range(1,255);
}
Would I need to change var number to a static variable?
Answer by darthbator · May 31, 2013 at 11:52 PM
Are the blocks touching? If they are you can use the collision system. If they're not touching just shoot rays out of all the sides. You can then access the objects off of the hit data. Setting it static would mean that it would be the same across all instances of the class. So if you changed it in one object it would change in all of them. I don't feel like that's what you're after, hence my suggestion to use rays or collisions and then use GetComponent to access the remote var.
So far I have this: var number : float;
function Start() {
number = Random.Range(1,255);
}
function Update() {
var hit : RaycastHit;
var ray = new Ray (transform.position, transform.forward);
if (Physics.Raycast (ray, hit, 100.0))
{
if(hit.transform.tag == "Cube"){
var numberscript = hit.transform.GetComponent(RandomNumber);
var OtherNumber = numberscript.number;
Debug.Log ("Hit Object");
}
}
}
I'm getting an error at var OtherNumber = numberscript.number; that says this:
NullReferenceException: Object reference not set to an instance of an object RandomNumber.Update () (at Assets/Game/RandomNumber.js:15)
Answer by jerichaosymphony · Jun 01, 2013 at 06:59 AM
Tag all squares/cubes with a word like "square", then do :
function findclosest(){
var allsquares = findallwithtag"square";
for (var square in allsquares){
if (distance (current square,square)<(diameter of squares*1.5))
}
(pseudocode) then you have the closest, you can make an array of the gameobjects, then make a new loop that compares the value of centre to all neighbours.
Your answer
Follow this Question
Related Questions
For Loop isn't working properly! 1 Answer
Algorithm to script 0 Answers
Room generator spawn 1 unique room 1 Answer
A fast triangle triangle intersection algorithm for unity? 4 Answers
pre generated world 0 Answers