- Home /
Why can't i use these arrays to access positions and objects properties? Please help!
Hello,
I am trying to make a game like snake (where you collect little spheres and it gets added to your tail and if you run into a wall, yourself, or an enemy you die) However I don't wanna die when I run into stuff and I don't want the spheres to follow behind me. Instead I want them to be able to be put into a predetermined structure (say like a shape made of dots (a cube, triangle, whatever)). Then when I shoot an enemy cube it will go run over to the next position. Once it's close enough I wanna snap its position to the position of where it should be. (My current approach is to have all the cubes already made into the structure but then I have the renderer turn to false for all the cubes except the first one you start with. Once the enemy cube floats over to the "invisible" structure cube it will snap to that position, be destroyed and then the structure cube will have it's render turned to true so we can see it. It doesn't deal with really any annoying parenting or anything like that, but I need help. I want to connect sort of a script of variables and information to the first structure cube and a health script to the enemies that also have the data of where they need to go. Here's what I've tried maybe someone could help me with where I went wrong? I'm pretty stuck....
Here's the informational code for the first structure cube.
static var VisibleThings : new Array(); static var InvisibleThings : new Array(); static var PositionOfThings: new Array(); static var ITLength : int; static var VTLength : int; static var ActiveThing : int;
static var thing0 : GameObject; static var thing1 : GameObject; static var thing2 : GameObject; static var thing3 : GameObject; static var thing4 : GameObject;
static var thing0pos : Vector3; static var thing1pos : Vector3; static var thing2pos : Vector3; static var thing3pos : Vector3; static var thing4pos : Vector3;
function Start () { thing0=gameObject.Find("Cube0"); thing0=gameObject.Find("Cube1"); thing0=gameObject.Find("Cube2"); thing0=gameObject.Find("Cube3"); thing0=gameObject.Find("Cube4");
InvisibleThings.Add("thing1", "thing2", "thing3", "thing4");
VisibleThings.Add("thing0");
PositionOfThings.Add("thing0pos", "thing1pos", "thing2pos", "thing3pos","thing4pos");
}
function Update() { thing0pos=thing0.transform.position; thing1pos=thing1.transform.position; thing2pos=thing2.transform.position; thing3pos=thing3.transform.position; thing4pos=thing4.transform.position;
ITLength=InvisibleThings.length;
VTLength=VisibleThings.length;
ActiveThing= InvisibleThings[VTLength - 1];
}
Then I have this for the Enemy's health:
var Enemy : Transform; var EnemyHealth= 50; var Speed= 0.5; var DistApart= Vector3.Distance(gameObject.position, Appear.ActiveThing.position); static var dead : boolean;
function OnCollisionEnter (collision : Collision) {
dead= false;
if(collision.gameObject.Find("Bullet(Clone)") && EnemyHealth > 0){
EnemyHealth= EnemyHealth - 10;
Debug.Log(EnemyHealth);
}
if(EnemyHealth==0)
{
dead= true;
}
}
function Update() { if(dead != false && DistApart > 1.9) { Enemy.transform.LookAt(Appear.ActiveThing); gameObject.transform.Translate(Vector3.forward * Speed, Space.Self); }
if(dead != false && DistApart <= 1.9)
{
gameObject.rigidbody.MovePosition(Appear.ActiveThing.position);
EnemyHealth= EnemyHealth + 10;
dead= false;
Appear.ActiveThing.renderer.enabled=true;
}
}
Any ideas of what I could do or what I am doing wrong? Any help is greatly appreciated! Thank you all in advance! If any additional information is needed just let me know.
Answer by skovacs1 · Jul 12, 2010 at 04:55 PM
As noted in the other answers, there are several things wrong in this code which have little or nothing to do with the logic being applied in the code but many very basic coding errors. I suggest you take the time to learn some programming from books, school and studying other scripts as you can avail yourself of the time and resources - there are a few too many errors that are too rudimentary for most people to spend the time to sort out what you're actually trying to do, let alone why it is failing.
Please use the scripting reference. Much of this information can be found there with very little searching.
Firstly, to provide the type of an array it would be something like:
static var VisibleThings : Array;
function Start() { VisibleThings = new Array(); //Size can be specified with new Array(Size); //... Other stuff }
or if you don't intend to resize, you could even specify the type of content like:
static var VisibleThings : GameObject[];
function Start() { VisibleThings = new GameObject[Size]; //...Other stuff }
Next, in stead of adding the string literals of "thing1", "thing2", "thing3", "thing4", you probably intend to add the objects or rather references to them like:
VisibleThings.Add(thing0);
Also note that you can initialize your arrays with values rather than calling Add in your Start function:
static var VisibleThings : Array;
function Start() { //...Setup thing0 VisibleThings = new Array(thing0); //... Other stuff }
Also, you specify ActiveThing to be an integer, but you then get the object from an array which is not an integer at the line ActiveThing= InvisibleThings[VTLength - 1]; - What are you trying to do here? You later use ActiveThing like an object in the enemy script - is it an int or isn't it?
I'm not sure if this was just omitted from the submitted code or not, but you're doing some very strange things here. First you declare some global vector3's thing0pos, etc. which only have default values and then you add them to an array (why? they're default values. The Vector3's you're storing are copied values, not the actual objects), but you never use them in the array, even though you seem to care about your array sizes shortly thereafter. You store strings of your variable names (which you probably mean to be references) and then you never even use them.
Consider this:
static var things : Array = new Array(5);
//These arrays store indices rather than objects static var visible : Array = new Array(0); static var hidden : Array = new Array(1,2,3,4); static var active : int; //is this an int //var active : GameObject; //or is this a GameOject? //Why not make active a vector3 called activePos?
function Start() { //I believe GameObject is a reference type, otherwise store a reference type for(var i : int = 0; i < 5; i++) { things.Add(GameObject.Find("Cube"+i)); } }
function Update() { //The positions of the GameObjects are already up to date //To loop through them and do stuff, do the following: //for (var thing : GameObject in things) { /do stuff with thing/ } //or to only affect a subset from the above arrays: //for (var index : int in visible) { /do stuff with things[index]/ }
//Why store the length locally when it is already stored in Array.length?
//What is this for? Does it need to be done every frame?
active = hidden[visible.length-1]; //if an int
//active = things[hidden[visible.length-1]]; //if a GO
//activePos = things[hidden[visible.length-1]].transform.position; // if a Vector3
}
Next is your enemy collision script:
What is this script attached to?
//If this is attached to the enemies, then you needn't store its transform //var Enemy : Transform; var health : int= 50; var speed : float = 0.5;
//wasn't active an int? Why were you using it like a GO? var dist : float = Vector3.Distance(transform.position, Appear.things[Appear.active].transform.position);
//if active is a GO //var dist : float = Vector3.Distance(transform.position, // Appear.active.transform.position);
//if using the Vector3 activePos //var dist : float = Vector3.Distance(transform.position, Appear.activePos;
//Why not use the less costly distance squared? like: //Vector3.sqrMagnitude(Appear.activePos - transform.position);
var dead : boolean = false;
function OnCollisionEnter (collision : Collision) { //Why find the object when you know what collided? Tag your bullets if (!dead && collision.gameObject.tag == "Bullet") { health -= 10; Debug.Log(health); if ( health <= 0) { dead = true; } } }
function Update() { if(dead) { //Dead, check the distance //if using distance squared check against 3.61 if(dist > 1.9) {
//Wasn't Enemy a Tranform already? What are you doing? //Enemy.transform.LookAt(Appear.ActiveThing); transform.LookAt(Appear.things[Appear.active].transform.position); //transform.LookAt(Appear.active.transform.position); //if a GO //transform.LookAt(Appear.activePos); //with the Vector3 method
transform.Translate(Vector3.forward * speed, Space.Self);
}
else
{
//Why are you only moving the rigidBody?
rigidbody.MovePosition(Appear.things[Appear.active].transform.position);
//rigidbody.MovePosition(Appear.active.transform.position); //If it is a GO
//It revives and heals when it it beyond a certain distance
if(health < 50)
{
health += 10;
if(health > 50)
{
health = 50;
}
}
dead= false;
//Did you even set this to false? If not, you can just store the position
Appear.things[appear.active].renderer.enabled=true;
//Appear.active.renderer.enabled=true; //if a GO
}
}
}
In short, this code is riddled with problems and I don't even know where to start helping you. Please revise, examining your use case and perform a series of smaller scale tests such as moving an object, shooting an object, showing/hiding objects and then combine them.
Many of these problems have already been solved in other questions and the forums. It is better to break your problem into separate questions as per the FAQ "Break multi-part problems into simpler questions". I recommend you delete this question and after some review consider several smaller more specific questions if you still cannot solve your problem or find your answers already in existence by then.
Thank you very much, I will continue to work on it and take the advice you have provided. It is greatly appreciated!
Answer by Eric5h5 · Jul 11, 2010 at 11:18 PM
Are you sure you want to add strings to the arrays, instead of references to the actual objects?
Answer by Daniel 6 · Jul 11, 2010 at 09:47 PM
One issue I noticed is that you are declaring your VisibleThings and InvisibleThings arrays incorrectly. Change the ":" to an "=" sign.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064419im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array variables structure question 1 Answer
how to show\hide dropdown menu in inspector? 0 Answers
How to set the position of an object using variables. 1 Answer
Position Array 1 Answer