Instantiate Object using Vector3 at posision?
hey gang Made a simple scriptwere when you Kill a Bunch of enemies and when their are no enemies in the group it finds the last Vector3 of the last enemy and then Instantiates and Item, Just wondering how you use a Vector3 as the posisoin rather then using Transform.posision, ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquadrinKill : MonoBehaviour {
public GameObject[] Squadrin;
public GameObject Item;
public Vector3 ItemSpawnpoint;
// Update is called once per frame
void Update () {
// Get Last posision of Lsat Squadrin Member
if (Squadrin.Length == 1) {
ItemSpawnpoint = Squadrin [1].transform.position;
}
if (Squadrin == null) {
GameObject Clone = Instantiate (Item, ItemSpawnpoint);
}
}
}
Transform.position is a Vector3, so the question about "using a Vector2 rather than Transform.position" doesn't really make sense. In any case, what you're doing with respect to those things looks basically correct.
There are two glaring problems with the code though...
1) If Squadrin.Length is 1, then it doesn't have an element with index 1 (because they start at zero). Therefore if line 18 is reached, it is guaranteed to throw an index out of bounds exception.
2) At the end you check if Squadrin is null, but you tried to access Squadrin.Length in the previous "if" statement. So if Squadrin were null, it would have thrown a null reference exception before it even got to your null test.
Answer by hugodeheld · Feb 21, 2018 at 12:00 PM
I think this should do it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquadrinKill : MonoBehaviour {
public List <GameObject> Squadrin;
public GameObject Item;
public Vector3 ItemSpawnpoint;
// Update is called once per frame
void Update () {
for(int i = 0; i < Squadrin.Count; i++){
if(Squadrin[i] == null){
Squadrin.RemoveAt(i)
i--;
}
}
if (Squadrin.Count == 1) {
Debug.Log ("PRINT pOSISION FOR iTEM" + ItemSpawnpoint); // Not Being Printed
ItemSpawnpoint = Squadrin[0].transform.position;
}
// Get Last posision of Lsat Squadrin Member
if (Squadrin.Count == 0) {
GameObject Clone = Instantiate (Item, ItemSpawnpoint, Quaternion.identity); // Not Instaniating
Destroy (this);
}
}
}
Instantiate takes 3 arguments if you want to create an object at a position. Also you should destory the object after otherwise it wil keep spawning the object
In line with code ItemSpawnpoint = Squadrin [1].transform.position;
there is an error. You are checking Count with 1, but numeration in array performs from 0, so in this case you`ll be trying to access 2nd array element. Change to Squadrin [0]
or Squadrin.First()
Thanks for the replies guys, cleared some things up, howerever after setting everything up the Item dose not instantiate in the game and has been assigned in the inspector :)
Your answer
Follow this Question
Related Questions
Instantiate ignored Vector3 0 Answers
[C#] Object not spawning where it should? 0 Answers
[SOLVED] Remove Vector3 Array step by step when GameObject spawned 1 Answer
Instantiate() Argument error 1 Answer
i get error when i add vector 3 0 Answers