Why aren't the vectors carrying over into an other function?












0















when I assign vectors to the array, the will not be carried over to the rocketUpdate function. The Gene array that is being used comes from an other script running on the gameObjects from the rocket array. This is the script it is from.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rockets : MonoBehaviour {

public Vector2 Gene;

public void Start()
{
Gene = new Vector2[10];
}
}


this is the main "controller" script.



using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RocketController : MonoBehaviour {

public int XVelocityMultiplier;
public int YVelocityMultiplier;

int lifespanSec;

int Count;

public int Size;
public GameObject rockets;
public GameObject RocketPrefab;

public System.Random rnd = new System.Random();
float x;
float y;

void Start()
{
rockets = new GameObject[Size];

lifespanSec = RocketPrefab.GetComponent<Rockets>().Gene.Length;

Invoke("killRockets", lifespanSec);

for (int i = 0; i < rockets.Length; i++)
{
GameObject rocketObject = Instantiate(RocketPrefab);
rocketObject.GetComponent<Rigidbody>().position = new Vector3(0, -4, 30);
rocketObject.name = "Rocket_" + (i+1);

for (int j = 0; j < rocketObject.GetComponent<Rockets>().Gene.Length; j++)
{
x = Convert.ToSingle(rnd.NextDouble() * (2 * XVelocityMultiplier) + XVelocityMultiplier * (rnd.Next(-1,1) + 0.1f));
y = Convert.ToSingle(rnd.NextDouble() * (YVelocityMultiplier));
rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);
Debug.Log(rocketObject.GetComponent<Rockets>().Gene[j]);
}
rockets[i] = rocketObject;

}
InvokeRepeating("RocketUpdate", 0, 1);
}

void Update()
{
if (Count == lifespanSec)
{
Count = 0;
}
}

void RocketUpdate()
{
Debug.Log(rockets[1].GetComponent<Rockets>().Gene[Count]);
if (rockets[0] != null)
{
for (int i = 0; i < rockets.Length; i++)
{
rockets[i].GetComponent<Rigidbody>().velocity = rockets[i].GetComponent<Rockets>().Gene[Count];
}
Debug.Log(rockets[1].GetComponent<Rockets>().Gene[Count]);
}
Debug.Log(Count);
Count++;
}

void killRockets()
{
for(int i = 0; i < rockets.Length; i++)
{
Destroy(rockets[i]);
}
}
}


when I run the first Debug.log() in the start function, every gameObject has its values. but when I run the same Debug.log in rocketUpdate() it suddenly doesn't have its values anymore. I have been stuck on the same problem for a long time. if anyone knows the problem please do tell.










share|improve this question


















  • 1





    A sidenote: in general try to avoid to call GetComponent over and over again. Instead store and reuse the reference once you got it the first time. You could e.g. already make the rockets of type Rockets or why not using List<Rocket>()? The huge advantage of lists is that you can extend them on the fly.

    – derHugo
    Nov 26 '18 at 16:43













  • Culd you explain a bit further what your entire code is supposed to do exactly .. I'm still trying to figure out everything that's supposed to happen here

    – derHugo
    Nov 26 '18 at 19:56











  • I was going to create an AI. but first I needed to create the script that just runs through an array of vectors and sets the rigidbodies to those vectors

    – Tijn van Veghel
    Nov 27 '18 at 10:43
















0















when I assign vectors to the array, the will not be carried over to the rocketUpdate function. The Gene array that is being used comes from an other script running on the gameObjects from the rocket array. This is the script it is from.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rockets : MonoBehaviour {

public Vector2 Gene;

public void Start()
{
Gene = new Vector2[10];
}
}


this is the main "controller" script.



using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RocketController : MonoBehaviour {

public int XVelocityMultiplier;
public int YVelocityMultiplier;

int lifespanSec;

int Count;

public int Size;
public GameObject rockets;
public GameObject RocketPrefab;

public System.Random rnd = new System.Random();
float x;
float y;

void Start()
{
rockets = new GameObject[Size];

lifespanSec = RocketPrefab.GetComponent<Rockets>().Gene.Length;

Invoke("killRockets", lifespanSec);

for (int i = 0; i < rockets.Length; i++)
{
GameObject rocketObject = Instantiate(RocketPrefab);
rocketObject.GetComponent<Rigidbody>().position = new Vector3(0, -4, 30);
rocketObject.name = "Rocket_" + (i+1);

for (int j = 0; j < rocketObject.GetComponent<Rockets>().Gene.Length; j++)
{
x = Convert.ToSingle(rnd.NextDouble() * (2 * XVelocityMultiplier) + XVelocityMultiplier * (rnd.Next(-1,1) + 0.1f));
y = Convert.ToSingle(rnd.NextDouble() * (YVelocityMultiplier));
rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);
Debug.Log(rocketObject.GetComponent<Rockets>().Gene[j]);
}
rockets[i] = rocketObject;

}
InvokeRepeating("RocketUpdate", 0, 1);
}

void Update()
{
if (Count == lifespanSec)
{
Count = 0;
}
}

void RocketUpdate()
{
Debug.Log(rockets[1].GetComponent<Rockets>().Gene[Count]);
if (rockets[0] != null)
{
for (int i = 0; i < rockets.Length; i++)
{
rockets[i].GetComponent<Rigidbody>().velocity = rockets[i].GetComponent<Rockets>().Gene[Count];
}
Debug.Log(rockets[1].GetComponent<Rockets>().Gene[Count]);
}
Debug.Log(Count);
Count++;
}

void killRockets()
{
for(int i = 0; i < rockets.Length; i++)
{
Destroy(rockets[i]);
}
}
}


when I run the first Debug.log() in the start function, every gameObject has its values. but when I run the same Debug.log in rocketUpdate() it suddenly doesn't have its values anymore. I have been stuck on the same problem for a long time. if anyone knows the problem please do tell.










share|improve this question


















  • 1





    A sidenote: in general try to avoid to call GetComponent over and over again. Instead store and reuse the reference once you got it the first time. You could e.g. already make the rockets of type Rockets or why not using List<Rocket>()? The huge advantage of lists is that you can extend them on the fly.

    – derHugo
    Nov 26 '18 at 16:43













  • Culd you explain a bit further what your entire code is supposed to do exactly .. I'm still trying to figure out everything that's supposed to happen here

    – derHugo
    Nov 26 '18 at 19:56











  • I was going to create an AI. but first I needed to create the script that just runs through an array of vectors and sets the rigidbodies to those vectors

    – Tijn van Veghel
    Nov 27 '18 at 10:43














0












0








0








when I assign vectors to the array, the will not be carried over to the rocketUpdate function. The Gene array that is being used comes from an other script running on the gameObjects from the rocket array. This is the script it is from.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rockets : MonoBehaviour {

public Vector2 Gene;

public void Start()
{
Gene = new Vector2[10];
}
}


this is the main "controller" script.



using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RocketController : MonoBehaviour {

public int XVelocityMultiplier;
public int YVelocityMultiplier;

int lifespanSec;

int Count;

public int Size;
public GameObject rockets;
public GameObject RocketPrefab;

public System.Random rnd = new System.Random();
float x;
float y;

void Start()
{
rockets = new GameObject[Size];

lifespanSec = RocketPrefab.GetComponent<Rockets>().Gene.Length;

Invoke("killRockets", lifespanSec);

for (int i = 0; i < rockets.Length; i++)
{
GameObject rocketObject = Instantiate(RocketPrefab);
rocketObject.GetComponent<Rigidbody>().position = new Vector3(0, -4, 30);
rocketObject.name = "Rocket_" + (i+1);

for (int j = 0; j < rocketObject.GetComponent<Rockets>().Gene.Length; j++)
{
x = Convert.ToSingle(rnd.NextDouble() * (2 * XVelocityMultiplier) + XVelocityMultiplier * (rnd.Next(-1,1) + 0.1f));
y = Convert.ToSingle(rnd.NextDouble() * (YVelocityMultiplier));
rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);
Debug.Log(rocketObject.GetComponent<Rockets>().Gene[j]);
}
rockets[i] = rocketObject;

}
InvokeRepeating("RocketUpdate", 0, 1);
}

void Update()
{
if (Count == lifespanSec)
{
Count = 0;
}
}

void RocketUpdate()
{
Debug.Log(rockets[1].GetComponent<Rockets>().Gene[Count]);
if (rockets[0] != null)
{
for (int i = 0; i < rockets.Length; i++)
{
rockets[i].GetComponent<Rigidbody>().velocity = rockets[i].GetComponent<Rockets>().Gene[Count];
}
Debug.Log(rockets[1].GetComponent<Rockets>().Gene[Count]);
}
Debug.Log(Count);
Count++;
}

void killRockets()
{
for(int i = 0; i < rockets.Length; i++)
{
Destroy(rockets[i]);
}
}
}


when I run the first Debug.log() in the start function, every gameObject has its values. but when I run the same Debug.log in rocketUpdate() it suddenly doesn't have its values anymore. I have been stuck on the same problem for a long time. if anyone knows the problem please do tell.










share|improve this question














when I assign vectors to the array, the will not be carried over to the rocketUpdate function. The Gene array that is being used comes from an other script running on the gameObjects from the rocket array. This is the script it is from.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rockets : MonoBehaviour {

public Vector2 Gene;

public void Start()
{
Gene = new Vector2[10];
}
}


this is the main "controller" script.



using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RocketController : MonoBehaviour {

public int XVelocityMultiplier;
public int YVelocityMultiplier;

int lifespanSec;

int Count;

public int Size;
public GameObject rockets;
public GameObject RocketPrefab;

public System.Random rnd = new System.Random();
float x;
float y;

void Start()
{
rockets = new GameObject[Size];

lifespanSec = RocketPrefab.GetComponent<Rockets>().Gene.Length;

Invoke("killRockets", lifespanSec);

for (int i = 0; i < rockets.Length; i++)
{
GameObject rocketObject = Instantiate(RocketPrefab);
rocketObject.GetComponent<Rigidbody>().position = new Vector3(0, -4, 30);
rocketObject.name = "Rocket_" + (i+1);

for (int j = 0; j < rocketObject.GetComponent<Rockets>().Gene.Length; j++)
{
x = Convert.ToSingle(rnd.NextDouble() * (2 * XVelocityMultiplier) + XVelocityMultiplier * (rnd.Next(-1,1) + 0.1f));
y = Convert.ToSingle(rnd.NextDouble() * (YVelocityMultiplier));
rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);
Debug.Log(rocketObject.GetComponent<Rockets>().Gene[j]);
}
rockets[i] = rocketObject;

}
InvokeRepeating("RocketUpdate", 0, 1);
}

void Update()
{
if (Count == lifespanSec)
{
Count = 0;
}
}

void RocketUpdate()
{
Debug.Log(rockets[1].GetComponent<Rockets>().Gene[Count]);
if (rockets[0] != null)
{
for (int i = 0; i < rockets.Length; i++)
{
rockets[i].GetComponent<Rigidbody>().velocity = rockets[i].GetComponent<Rockets>().Gene[Count];
}
Debug.Log(rockets[1].GetComponent<Rockets>().Gene[Count]);
}
Debug.Log(Count);
Count++;
}

void killRockets()
{
for(int i = 0; i < rockets.Length; i++)
{
Destroy(rockets[i]);
}
}
}


when I run the first Debug.log() in the start function, every gameObject has its values. but when I run the same Debug.log in rocketUpdate() it suddenly doesn't have its values anymore. I have been stuck on the same problem for a long time. if anyone knows the problem please do tell.







c# arrays unity3d vector






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 10:13









Tijn van VeghelTijn van Veghel

1




1








  • 1





    A sidenote: in general try to avoid to call GetComponent over and over again. Instead store and reuse the reference once you got it the first time. You could e.g. already make the rockets of type Rockets or why not using List<Rocket>()? The huge advantage of lists is that you can extend them on the fly.

    – derHugo
    Nov 26 '18 at 16:43













  • Culd you explain a bit further what your entire code is supposed to do exactly .. I'm still trying to figure out everything that's supposed to happen here

    – derHugo
    Nov 26 '18 at 19:56











  • I was going to create an AI. but first I needed to create the script that just runs through an array of vectors and sets the rigidbodies to those vectors

    – Tijn van Veghel
    Nov 27 '18 at 10:43














  • 1





    A sidenote: in general try to avoid to call GetComponent over and over again. Instead store and reuse the reference once you got it the first time. You could e.g. already make the rockets of type Rockets or why not using List<Rocket>()? The huge advantage of lists is that you can extend them on the fly.

    – derHugo
    Nov 26 '18 at 16:43













  • Culd you explain a bit further what your entire code is supposed to do exactly .. I'm still trying to figure out everything that's supposed to happen here

    – derHugo
    Nov 26 '18 at 19:56











  • I was going to create an AI. but first I needed to create the script that just runs through an array of vectors and sets the rigidbodies to those vectors

    – Tijn van Veghel
    Nov 27 '18 at 10:43








1




1





A sidenote: in general try to avoid to call GetComponent over and over again. Instead store and reuse the reference once you got it the first time. You could e.g. already make the rockets of type Rockets or why not using List<Rocket>()? The huge advantage of lists is that you can extend them on the fly.

– derHugo
Nov 26 '18 at 16:43







A sidenote: in general try to avoid to call GetComponent over and over again. Instead store and reuse the reference once you got it the first time. You could e.g. already make the rockets of type Rockets or why not using List<Rocket>()? The huge advantage of lists is that you can extend them on the fly.

– derHugo
Nov 26 '18 at 16:43















Culd you explain a bit further what your entire code is supposed to do exactly .. I'm still trying to figure out everything that's supposed to happen here

– derHugo
Nov 26 '18 at 19:56





Culd you explain a bit further what your entire code is supposed to do exactly .. I'm still trying to figure out everything that's supposed to happen here

– derHugo
Nov 26 '18 at 19:56













I was going to create an AI. but first I needed to create the script that just runs through an array of vectors and sets the rigidbodies to those vectors

– Tijn van Veghel
Nov 27 '18 at 10:43





I was going to create an AI. but first I needed to create the script that just runs through an array of vectors and sets the rigidbodies to those vectors

– Tijn van Veghel
Nov 27 '18 at 10:43












1 Answer
1






active

oldest

votes


















0














Several things. In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. I'm not sure if that's what you want, but it's what it is. In order to populate it with data, you have to explicitly tell it what the data should be by creating a reference to the script holding the actual values. For example,



private MainScript whateverYouWannaCallIt;
private Vector2 geneCopies;

public void Awake()
{
whateverYouWannaCallIt = gameObject.GetComponent<MainScript>();

for (int i = 0; i < whateverYouWannaCallIt.rockets.Length; i++)
{
geneCopies[i] = whateverYouWannaCallIt.rockets.gene[i];
}
}


You should also initialize a List and populate it with those rockets and maybe one for the rigidbodies as well, those getComponent calls are gonna contribute a lot to your overhead.



Also, I'm intrigued. What is your goal with this script? And what's your game about?






share|improve this answer
























  • I was going to create an AI. but I am running into a lot of roadblocks so it isn't running very smoothly. in your code the first line, my c# script doesn't recognize the type MainScript. do I have to generate a new type? or can I use the type Component

    – Tijn van Veghel
    Nov 27 '18 at 10:42











  • In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. .. as far as I can see the array is filled with rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);. I also see no MainScript in the question. You should also initialize a List and populate it with those rockets .. isn't this what the public GameObject rockets; is for?

    – derHugo
    Nov 27 '18 at 17:45











  • He has a small snippet from an auxiliary script at the top called rockets and a larger main script below it called rocketController. In the first script there is only the creation of an array, nothing else. You are right about the rockets array though, but he should still use another array for each rocket's rigidbody to avoid those getcomponent calls every second @derHugo

    – jayj593
    Nov 27 '18 at 18:53













  • Yeah you need to rename all these things to match your variable names, and where I say MainScript, you need to name it exactly what your class is named otherwise it will not be able to find it.

    – jayj593
    Nov 27 '18 at 18:59











  • and it's not every second but every frame ;)

    – derHugo
    Nov 27 '18 at 21:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478896%2fwhy-arent-the-vectors-carrying-over-into-an-other-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Several things. In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. I'm not sure if that's what you want, but it's what it is. In order to populate it with data, you have to explicitly tell it what the data should be by creating a reference to the script holding the actual values. For example,



private MainScript whateverYouWannaCallIt;
private Vector2 geneCopies;

public void Awake()
{
whateverYouWannaCallIt = gameObject.GetComponent<MainScript>();

for (int i = 0; i < whateverYouWannaCallIt.rockets.Length; i++)
{
geneCopies[i] = whateverYouWannaCallIt.rockets.gene[i];
}
}


You should also initialize a List and populate it with those rockets and maybe one for the rigidbodies as well, those getComponent calls are gonna contribute a lot to your overhead.



Also, I'm intrigued. What is your goal with this script? And what's your game about?






share|improve this answer
























  • I was going to create an AI. but I am running into a lot of roadblocks so it isn't running very smoothly. in your code the first line, my c# script doesn't recognize the type MainScript. do I have to generate a new type? or can I use the type Component

    – Tijn van Veghel
    Nov 27 '18 at 10:42











  • In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. .. as far as I can see the array is filled with rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);. I also see no MainScript in the question. You should also initialize a List and populate it with those rockets .. isn't this what the public GameObject rockets; is for?

    – derHugo
    Nov 27 '18 at 17:45











  • He has a small snippet from an auxiliary script at the top called rockets and a larger main script below it called rocketController. In the first script there is only the creation of an array, nothing else. You are right about the rockets array though, but he should still use another array for each rocket's rigidbody to avoid those getcomponent calls every second @derHugo

    – jayj593
    Nov 27 '18 at 18:53













  • Yeah you need to rename all these things to match your variable names, and where I say MainScript, you need to name it exactly what your class is named otherwise it will not be able to find it.

    – jayj593
    Nov 27 '18 at 18:59











  • and it's not every second but every frame ;)

    – derHugo
    Nov 27 '18 at 21:14
















0














Several things. In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. I'm not sure if that's what you want, but it's what it is. In order to populate it with data, you have to explicitly tell it what the data should be by creating a reference to the script holding the actual values. For example,



private MainScript whateverYouWannaCallIt;
private Vector2 geneCopies;

public void Awake()
{
whateverYouWannaCallIt = gameObject.GetComponent<MainScript>();

for (int i = 0; i < whateverYouWannaCallIt.rockets.Length; i++)
{
geneCopies[i] = whateverYouWannaCallIt.rockets.gene[i];
}
}


You should also initialize a List and populate it with those rockets and maybe one for the rigidbodies as well, those getComponent calls are gonna contribute a lot to your overhead.



Also, I'm intrigued. What is your goal with this script? And what's your game about?






share|improve this answer
























  • I was going to create an AI. but I am running into a lot of roadblocks so it isn't running very smoothly. in your code the first line, my c# script doesn't recognize the type MainScript. do I have to generate a new type? or can I use the type Component

    – Tijn van Veghel
    Nov 27 '18 at 10:42











  • In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. .. as far as I can see the array is filled with rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);. I also see no MainScript in the question. You should also initialize a List and populate it with those rockets .. isn't this what the public GameObject rockets; is for?

    – derHugo
    Nov 27 '18 at 17:45











  • He has a small snippet from an auxiliary script at the top called rockets and a larger main script below it called rocketController. In the first script there is only the creation of an array, nothing else. You are right about the rockets array though, but he should still use another array for each rocket's rigidbody to avoid those getcomponent calls every second @derHugo

    – jayj593
    Nov 27 '18 at 18:53













  • Yeah you need to rename all these things to match your variable names, and where I say MainScript, you need to name it exactly what your class is named otherwise it will not be able to find it.

    – jayj593
    Nov 27 '18 at 18:59











  • and it's not every second but every frame ;)

    – derHugo
    Nov 27 '18 at 21:14














0












0








0







Several things. In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. I'm not sure if that's what you want, but it's what it is. In order to populate it with data, you have to explicitly tell it what the data should be by creating a reference to the script holding the actual values. For example,



private MainScript whateverYouWannaCallIt;
private Vector2 geneCopies;

public void Awake()
{
whateverYouWannaCallIt = gameObject.GetComponent<MainScript>();

for (int i = 0; i < whateverYouWannaCallIt.rockets.Length; i++)
{
geneCopies[i] = whateverYouWannaCallIt.rockets.gene[i];
}
}


You should also initialize a List and populate it with those rockets and maybe one for the rigidbodies as well, those getComponent calls are gonna contribute a lot to your overhead.



Also, I'm intrigued. What is your goal with this script? And what's your game about?






share|improve this answer













Several things. In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. I'm not sure if that's what you want, but it's what it is. In order to populate it with data, you have to explicitly tell it what the data should be by creating a reference to the script holding the actual values. For example,



private MainScript whateverYouWannaCallIt;
private Vector2 geneCopies;

public void Awake()
{
whateverYouWannaCallIt = gameObject.GetComponent<MainScript>();

for (int i = 0; i < whateverYouWannaCallIt.rockets.Length; i++)
{
geneCopies[i] = whateverYouWannaCallIt.rockets.gene[i];
}
}


You should also initialize a List and populate it with those rockets and maybe one for the rigidbodies as well, those getComponent calls are gonna contribute a lot to your overhead.



Also, I'm intrigued. What is your goal with this script? And what's your game about?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 20:10









jayj593jayj593

313




313













  • I was going to create an AI. but I am running into a lot of roadblocks so it isn't running very smoothly. in your code the first line, my c# script doesn't recognize the type MainScript. do I have to generate a new type? or can I use the type Component

    – Tijn van Veghel
    Nov 27 '18 at 10:42











  • In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. .. as far as I can see the array is filled with rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);. I also see no MainScript in the question. You should also initialize a List and populate it with those rockets .. isn't this what the public GameObject rockets; is for?

    – derHugo
    Nov 27 '18 at 17:45











  • He has a small snippet from an auxiliary script at the top called rockets and a larger main script below it called rocketController. In the first script there is only the creation of an array, nothing else. You are right about the rockets array though, but he should still use another array for each rocket's rigidbody to avoid those getcomponent calls every second @derHugo

    – jayj593
    Nov 27 '18 at 18:53













  • Yeah you need to rename all these things to match your variable names, and where I say MainScript, you need to name it exactly what your class is named otherwise it will not be able to find it.

    – jayj593
    Nov 27 '18 at 18:59











  • and it's not every second but every frame ;)

    – derHugo
    Nov 27 '18 at 21:14



















  • I was going to create an AI. but I am running into a lot of roadblocks so it isn't running very smoothly. in your code the first line, my c# script doesn't recognize the type MainScript. do I have to generate a new type? or can I use the type Component

    – Tijn van Veghel
    Nov 27 '18 at 10:42











  • In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. .. as far as I can see the array is filled with rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);. I also see no MainScript in the question. You should also initialize a List and populate it with those rockets .. isn't this what the public GameObject rockets; is for?

    – derHugo
    Nov 27 '18 at 17:45











  • He has a small snippet from an auxiliary script at the top called rockets and a larger main script below it called rocketController. In the first script there is only the creation of an array, nothing else. You are right about the rockets array though, but he should still use another array for each rocket's rigidbody to avoid those getcomponent calls every second @derHugo

    – jayj593
    Nov 27 '18 at 18:53













  • Yeah you need to rename all these things to match your variable names, and where I say MainScript, you need to name it exactly what your class is named otherwise it will not be able to find it.

    – jayj593
    Nov 27 '18 at 18:59











  • and it's not every second but every frame ;)

    – derHugo
    Nov 27 '18 at 21:14

















I was going to create an AI. but I am running into a lot of roadblocks so it isn't running very smoothly. in your code the first line, my c# script doesn't recognize the type MainScript. do I have to generate a new type? or can I use the type Component

– Tijn van Veghel
Nov 27 '18 at 10:42





I was going to create an AI. but I am running into a lot of roadblocks so it isn't running very smoothly. in your code the first line, my c# script doesn't recognize the type MainScript. do I have to generate a new type? or can I use the type Component

– Tijn van Veghel
Nov 27 '18 at 10:42













In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. .. as far as I can see the array is filled with rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);. I also see no MainScript in the question. You should also initialize a List and populate it with those rockets .. isn't this what the public GameObject rockets; is for?

– derHugo
Nov 27 '18 at 17:45





In your first script you aren't populating an array with a bunch of values, you are creating a new array with space for 10 Vector2s. .. as far as I can see the array is filled with rocketObject.GetComponent<Rockets>().Gene[j] = new Vector2(x, y);. I also see no MainScript in the question. You should also initialize a List and populate it with those rockets .. isn't this what the public GameObject rockets; is for?

– derHugo
Nov 27 '18 at 17:45













He has a small snippet from an auxiliary script at the top called rockets and a larger main script below it called rocketController. In the first script there is only the creation of an array, nothing else. You are right about the rockets array though, but he should still use another array for each rocket's rigidbody to avoid those getcomponent calls every second @derHugo

– jayj593
Nov 27 '18 at 18:53







He has a small snippet from an auxiliary script at the top called rockets and a larger main script below it called rocketController. In the first script there is only the creation of an array, nothing else. You are right about the rockets array though, but he should still use another array for each rocket's rigidbody to avoid those getcomponent calls every second @derHugo

– jayj593
Nov 27 '18 at 18:53















Yeah you need to rename all these things to match your variable names, and where I say MainScript, you need to name it exactly what your class is named otherwise it will not be able to find it.

– jayj593
Nov 27 '18 at 18:59





Yeah you need to rename all these things to match your variable names, and where I say MainScript, you need to name it exactly what your class is named otherwise it will not be able to find it.

– jayj593
Nov 27 '18 at 18:59













and it's not every second but every frame ;)

– derHugo
Nov 27 '18 at 21:14





and it's not every second but every frame ;)

– derHugo
Nov 27 '18 at 21:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478896%2fwhy-arent-the-vectors-carrying-over-into-an-other-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks