Instead of using a list, I want to use a two-dimensional array of objects and a two-dimensional array of transforms.
But instantiations do not work with this code in C sharp:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class try_array_instantiate : MonoBehaviour {
//entity declaration
public GameObject[,] cube;
public GameObject cube_initial;
public GameObject[,] cubeCree;
public Transform[,] origine;
public Transform origine_initial;
public float moveSpeed=5f;
int i;
void Start() {
// take the transformation composantres of the initial cube
origine_initial=cube_initial.GetComponent<Transform>();
}
void Update() {
if (Input.GetKeyDown (KeyCode.Q)) {
//create the first shot duplication from the single initial cube
cube[1,1]=Instantiate (cubeCree[1,1], origine_initial.position, origine_initial.rotation) as GameObject;
cube[1,1].transform.Translate(Time.deltaTime*moveSpeed,0,Time.deltaTime*moveSpeed,Space.Self);
cube[1,2]=Instantiate (cubeCree[1,1], origine_initial.position, origine_initial.rotation) as GameObject;
cube[1,2].transform.Translate(Time.deltaTime*moveSpeed,0,-Time.deltaTime*moveSpeed,Space.Self);
//destruction of the initial cube
Destroy(cube_initial);
}
}
}
What should I modify in this code so that the instantations are created?
In fact, I can't adapt your correction at this code I would like to do initially which makes a duplicate shot with each press of the Q key.
Here's this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tir_duplique_pas_encore_bon : MonoBehaviour {
// If what we want to work with are Transforms, then store that reference directly.
public Transform[,] cubeInitial;
public Transform[,] cubePrefab;
public Transform cubePrefab0;
public Transform cubeInitial0;
public float moveSpeed = 5f;
int i;
int p=0;
// Private variables for the content we populate at runtime.
Transform[,] cubes;
Vector3 originPosition;
Quaternion originRotation;
void Start() {
// Initialize the array before we try to use it.
// Note that arrays are 0-based, so if we later want to access [1, 2],
// that means we need at least a 2x3 array (ie. {0, 1} x {0, 1, 2})
cubes = new Transform[100000,1];
cubeInitial = new Transform[100000,1];
cubePrefab = new Transform[100000,1];
cubePrefab0 = cubePrefab [0, 0];
cubeInitial0 = cubeInitial [0, 0];
}
void Update() {
if (Input.GetKeyDown (KeyCode.Q)) {
// Capture the transformation of our reference object before we delete it.
if(cubeInitial[0,0] != null) {
originPosition = cubeInitial[0,0].position;
originRotation = cubeInitial[0,0].rotation;
//Destroy(cubeInitial[0,0]);
//cubeInitial[0,0] = null;
}
// Instantiate our cube instances using the prefab.
cubes[0, 0] = Instantiate<Transform>(cubePrefab[0,0], originPosition, originRotation);
cubes[0, 1] = Instantiate<Transform>(cubePrefab[0,1], originPosition, originRotation);
// If you want these objects to move over time, then don't
// put the Translate() call inside a block that runs only
// when the player presses "Q". Move them every frame.
float step = Time.deltaTime * moveSpeed;
if(cubes[0,0] != null)
cubes[0,0].Translate(step, 0, step, Space.Self);
if(cubes[0,1] != null)
cubes[0,1].Translate(step, 0, -step, Space.Self);
}
if (Input.GetKeyDown (KeyCode.Q)) {
for (i=(int)Mathf.Pow (2, p+1)-1; i <=2*((int)Mathf.Pow (2, p+1)-1) ;i++ )
{if(cubeInitial[i-1,0] != null) {
originPosition = cubeInitial[i-1,0].position;
originRotation = cubeInitial[i-1,0].rotation;
Destroy(cubeInitial[i-1,0]);
cubeInitial[i-1,0] = null;
}
if(cubeInitial[i-1,1] != null) {
originPosition = cubeInitial[i-1,1].position;
originRotation = cubeInitial[i-1,1].rotation;
Destroy(cubeInitial[i-1,1]);
cubeInitial[i-1,1] = null;
}
cubes[i, 0] = Instantiate<Transform>(cubePrefab[i,0], originPosition, originRotation);
cubes[i, 1] = Instantiate<Transform>(cubePrefab[i,1], originPosition, originRotation);
float step = Time.deltaTime * moveSpeed;
if(cubes[i,0] != null)
cubes[0,0].Translate(step, 0, step, Space.Self);
if(cubes[i,1] != null)
cubes[i,1].Translate(step, 0, -step, Space.Self);
}
p++;
}
}
}
I try to apply your answer to my update code and I have problem here:
Instead of declaring transform cubePrefab
and cubeInitial
, I declare:
public transform[,] cubeInitial;
public transform[,] cubePrefab;
To adapt the following of my new code updated which must uses arrays to classify shoot which are multiplying
So,no possible to slip in the inspector in the script the firstcube(shoot)
like I did it with your first code thanks to:
public Transform cubeInitial;
public Transform cubePrefab;
After,i tried to to that:
public Transform[,] cubeInitial;
public Transform[,] cubePrefab;
public Transform cubePrefab0;
public Transform cubeInitial0;
and after:
cubes = new Transform[100000,1];
cubeInitial = new Transform[100000,1];
cubePrefab = new Transform[100000,1];
cubePrefab0 = cubePrefab [0, 0];
cubeInitial0 = cubeInitial [0, 0];
So, compile was good but shoot doesn't work on play unity
Then,to adapt your update,i write this last code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tir_duplique_pas_encore_bon : MonoBehaviour {
// If what we want to work with are Transforms, then store that reference directly.
public Transform[,] cubeInitial;
public Transform[,] cubePrefab;
public Transform cubePrefab0;
public Transform cubeInitial0;
public float moveSpeed = 5f;
int i;
int p=1;
// Private variables for the content we populate at runtime.
Transform[,] cubes;
Vector3 originPosition;
Quaternion originRotation;
void Start() {
// Initialize the array before we try to use it.
// Note that arrays are 0-based, so if we later want to access [1, 2],
// that means we need at least a 2x3 array (ie. {0, 1} x {0, 1, 2})
cubes = new Transform[100000,1];
cubeInitial = new Transform[100000,1];
cubePrefab = new Transform[100000,1];
cubePrefab0 = cubePrefab [0, 0];
cubeInitial0 = cubeInitial [0, 0];
}
void Update() {
if (Input.GetKeyDown (KeyCode.Q)) {
// Capture the transformation of our reference object before we delete it.
if(cubeInitial[0,0] != null) {
originPosition = cubeInitial[0,0].position;
originRotation = cubeInitial[0,0].rotation;
Destroy(cubeInitial[0,0]);
cubeInitial[0,0] = null;
}
// Instantiate our cube instances using the prefab.
cubes[0, 0] = Instantiate<Transform>(cubePrefab[0,0], originPosition, originRotation);
cubes[0, 1] = Instantiate<Transform>(cubePrefab[0,0], originPosition, originRotation);
// If you want these objects to move over time, then don't
// put the Translate() call inside a block that runs only
// when the player presses "Q". Move them every frame.
float step = Time.deltaTime * moveSpeed;
if(cubes[0,0] != null)
cubes[0,0].Translate(step, 0, step, Space.Self);
if(cubes[0,1] != null)
cubes[0,1].Translate(step, 0, -step, Space.Self);
}
if (Input.GetKeyDown (KeyCode.Q)) {
for (i=(int)Mathf.Pow (2, p); i <= (int)Mathf.Pow (2, p + 1) - 1;i++ )
{
if(cubeInitial[i,0] != null) {
originPosition = cubeInitial[i,0].position;
originRotation = cubeInitial[i,0].rotation;
Destroy(cubeInitial[i,0]);
cubeInitial[i,0] = null;
}
if(cubeInitial[i,1] != null) {
originPosition = cubeInitial[i,1].position;
originRotation = cubeInitial[i,1].rotation;
Destroy(cubeInitial[i,1]);
cubeInitial[i,1] = null;
}
cubes[i, 0] = Instantiate<Transform>(cubePrefab[i,0], originPosition, originRotation);
cubes[i, 1] = Instantiate<Transform>(cubePrefab[i,1], originPosition, originRotation);
float step = Time.deltaTime * moveSpeed;
if(cubes[i,0] != null)
cubes[0,0].Translate(step, 0, step, Space.Self);
if(cubes[i,1] != null)
cubes[i,1].Translate(step, 0, -step, Space.Self);
}
p++;
}
}
}
But it doesn't work on play Unity
Yes sorry the console of Unity indicate:
ArgumentException: The Object you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:151) UnityEngine.Object.Instantiate[Transform] (UnityEngine.Transform original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:206) tir_duplique_pas_encore_bon.Update () (at Assets/tir_duplique_pas_encore_bon.cs:54)
code source contains at the beginning:
cubePrefab0 = cubePrefab [0, 0]; cubeInitial0 = cubeInitial [0, 0];
so 2 fields appear in the script of the inspector. Then,in thes 2 fields,I slid another same cube in the hierarchy that has no script Is it no sufficient?
Instantiate()
is empty. Where in your project do you assign a value tocubePrefab[0,0]
before you try to spawn it? How do you expect the game engine to know what kind of object you want to spawn there? \$\endgroup\$