- Home /
Keep track of every time an int changes, including what the value is each time?
What is a good way to do this? I need to keep track of the players position every time he changes direction so my enemy ai will work.
Answer by Mmmpies · Feb 01, 2015 at 03:51 PM
OK your update 17 hours ago was a lot clearer. This will get a Vector3 from a list if called from the other script:
private List<Vector3> posList = new List<Vector3>();
private GetTheV3 myGetTheV3;
public GameObject ScriptHolder;
void Start()
{
posList.Add (new Vector3(1f, 1f, 1f));
posList.Add (new Vector3(2f, 1f, 2f));
posList.Add (new Vector3(3f, 1f, 3f));
posList.Add (new Vector3(4f, 1f, 4f));
posList.Add (new Vector3(5f, 1f, 5f));
myGetTheV3 = ScriptHolder.GetComponent<GetTheV3>();
}
public Vector3 GetV3(int myInt)
{
Vector3 myV3= (Vector3)posList[myInt];
return myV3;
}
public void SetV3()
{
float myX = transform.position.x;
float myY = transform.position.y;
float myZ = transform.position.z;
posList.Add(new Vector3(myX,myY,myZ));
myGetTheV3.AddToInt();
}
If you call that script array1 as your update said then in the other script.
using UnityEngine;
using System.Collections;
public class GetTheV3 : MonoBehaviour {
private array1 myArray1;
public GameObject ScriptHolder;
private int PubInt = 5;
void Start()
{
myArray1 = ScriptHolder.GetComponent<array1>();
}
void Update()
{
if(Input.GetKeyDown( KeyCode.Space))
GetMyV3();
}
void GetMyV3()
{
Vector3 gotV3 = myArray1.GetV3(Random.Range(0,PubInt));
Debug.Log(gotV3);
}
public void AddToInt()
{
PubInt++;
}
}
That's just example code the SetV3 in the first script can be called at any point from another script to record the current position, that's a guess at what you likely want and in my test would just record the same position as I put both scripts on an empty GameObject.
It just sets a random range but if you call the SetV3 that updates the int in the second script. Like I said just an example.
Answer by PhoenixBlackReal · Jan 31, 2015 at 09:15 PM
Here is a quick example, I'm sure you can work out from here:
using UnityEngine;
using System.Collections;
public class followvalue : MonoBehaviour {
Vector3 previous_value;
Vector3 current_value;
void Start (){
previous_value = this.gameObject.transform.position;;
current_value = this.gameObject.transform.position;;
}
void Update () {
previous_value = current_value;
current_value = this.gameObject.transform.position;
if(previous_value != current_value){
Debug.Log("gameobject changed position");
}
}
}
Note, position is a Vector3, not int.
I need a method that saves every single change of position, so im using an array :) however i need some help. how do i use a variable to fill in as the index of an index of another script?
first script
public ArrayList mylist;
void Start () {
mylist.Add (5);
mylist [0] = transform.position;
}
second script
private ArrayList mylist2;
private int myint = 1;
void Update () {
mylist2 = GetComponent <firstscript> ().mylist;
transform.position = mylist2 [myint];
}
thats what i tried, but it says cannot convert type object to vector3
@InvincibleCat do you know how to access a certain index number from another script?
In the Start() of your second script:
FirstScript firstScript;
void Start()
{
GameObject firstObject = GameObject.Find("name of object containing first script");
firstScript = firstObject.GetComponent<FirstScript>();
}
then in your update you should be able to get to the list by calling firstScript.mylist. You could also store a reference to firstScript.mylist in the Start() function (i.e. mylist2 = firstScript.mylist; in Start()).
Your answer
Follow this Question
Related Questions
Enemy Rigidbody Not Responding to Gravity 1 Answer
Problem with the movement of AI 1 Answer
Why do the AI get caught on trees? 0 Answers
Enemy is not chasing player. it is escaping. 1 Answer
Simple AI Error 8025 Parrsing error. 2 Answers