- Home /
Want replace a current object with one inside array the object being replaced with is prefab
so i have array with object inside i want replace object with whats inside array in slot 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class SpawnSetting1 : MonoBehaviour {
public bool opt;
public GameObject current;
public GameObject[] test;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
current = this.gameObject;
}
void OnGUI()
{
if(opt)
{
if(GUI.Button(new Rect(Screen.width - 160,0, 160, 30), "make sphere"))
{
current = test[1];
}
}
}
public void Options()
{
opt = !opt;
}
}
Answer by Hellium · Nov 14, 2017 at 05:45 PM
I haven't tested the script but give it a try
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class SpawnSetting1 : MonoBehaviour
{
public bool opt;
private GameObject current;
[SerializeField]
private GameObject[] prefabs;
private GameObject[] instances;
private void Awake()
{
instances = new GameObject[prefabs.Length] ;
SwapWith(0);
}
void OnGUI()
{
if(opt)
{
if(GUI.Button(new Rect(Screen.width - 160,0, 160, 30), "make sphere"))
{
SwapWith(1) ;
}
}
}
public void Options()
{
opt = !opt;
}
public void SwapWith( int prefabIndex )
{
if( instances[prefabIndex] == null )
{
instances[prefabIndex] = Instantiate( prefabs[prefabIndex] ) ;
}
if( current != null )
{
instances[prefabIndex].transform.position = current.transform.position ;
instances[prefabIndex].transform.rotation = current.transform.rotation ;
current.SetActive(false);
}
current = instances[prefabIndex];
current.SetActive(true);
}
}
INITIAL ANSWER
Supposing you want to hide the current one and show a new one :
if(GUI.Button(new Rect(Screen.width - 160,0, 160, 30), "make sphere"))
{
current.SetActive(false);
current = test[1];
current.SetActive(true);
}
test
must contain objects currently in the scene. If you have prefabs, the code won't be the same.
yes the objects are prefabs main goal is to not have them in the scene tell i set it be that object from the array
its also prefectly fine if code isnt same as long as it works idm restarting from scratch
I've updated my answer to handle prefabs, give it a try ;)
IndexOutOfRangeException: Array index is out of range. SpawnSetting1.SwapWith (Int32 prefabIndex) (at Assets/Game/Spawn/SpawnSetting1.cs:41) SpawnSetting1.OnGUI () (at Assets/Game/Spawn/SpawnSetting1.cs:29)
nope
if (instances[prefabIndex] == null) <---this is part with issue
I don't have the error here. Are you sure you have filled the prefabs
array?
Your answer
Follow this Question
Related Questions
What is the best way to instatiate an array of connected GameObjects? 0 Answers
How to randomly spawn three non repeating gameobjects from an array? 2 Answers
Save Gameobject in Array 1 Answer
How to put gameObjects to the list? 4 Answers
Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers