Void Start Acting Like Void Update
Hello Answers, I have been working a Ghost System and want to copy one list to another when the script starts, Weirdly enough, the command I setup on void start seems to be running itself over and over again at the same time as the other script is changing values.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GhostPlayer : MonoBehaviour {
public List<Vector2> playerMove;
public Player player;
void Start () {
playerMove = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ().playerPos;
}
void Update () {
}
}
Here is the code, Really have no Idea what is going on... Thanks in advance, NightLucidity
Answer by Statement · Oct 24, 2015 at 10:22 PM
You're not copying the list. You are referencing the list. If Player changes the list, so will GhostPlayer also get those changes. If you want to make a copy of it, do it like this:
void Start () {
var go = GameObject.FindGameObjectWithTag ("Player");
var original = go.GetComponent<Player> ();
// Create a new list with the contents of the other
playerMove = new List<Vector2>(original.playerPos);
}
Thank you for quick explanation and fix, Still I wasn't expecting that the reference of the list would constantly update it if it was in the start function, well I guess we learn new things every day, Thanks Again, NightLucidity
Your answer
![](https://koobas.hobune.stream/wayback/20220612042608im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Run script at game startup/run? 0 Answers
Why is a gameobject moves at start? 0 Answers
Having problem with instantiated prefabs 0 Answers
Unity error on a nonexistent line of code 1 Answer
How to get 60 Frames per second in iOS? 0 Answers