I want to instantiate “s1Button” but it has to be child of “ChoiceButtonHolder”.
How do I have to do this?
Instantiate(s1Button);
^This is what I have now
I want to instantiate “s1Button” but it has to be child of “ChoiceButtonHolder”.
How do I have to do this?
Instantiate(s1Button);
^This is what I have now
Just configure transform.parent after instantiating the object.
But how?
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
// manually add prefab in Unity Editor
public GameObject s1Button_prefab;
private GameObject s1Button;
// manually add in Unity Editor
public GameObject ChoiceButtonHolder;
private void Start()
{
s1Button = Instantiate(s1Button_prefab);
//Sets "ChoiceButtonHolder" as the new parent of the s1Button.
s1Button.transform.SetParent(ChoiceButtonHolder.transform);
}
}
Also you can read more about it :
https://docs.unity3d.com/ScriptReference/Transform.SetParent.html
I tried that but then I got this error,
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
UnityEngine.Transform:SetParent(Transform)
This error is coming up when you trying to change PREFAB’s parent. As you can see, we are setting parent of new GameObject s1Button.transform.SetParent(ChoiceButtonHolder.transform); . If you will attempt to change s1Button_prefab parent you will get error. You can share your code if you can’t fix it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChoiceSpawner : MonoBehaviour {
[SerializeField]
private GameObject PlayerComponents;
[SerializeField]
private GameObject s1Button_prefab;
private GameObject s1Button;
[SerializeField]
private GameObject s2Button;
public GameObject ChoiceButtonHolder;
public void Spawner(int RealNewSpell)
{
Debug.Log(RealNewSpell);
if (RealNewSpell == 1)
{
Debug.Log("test1");
s1Button = Instantiate(s1Button_prefab);
s1Button.transform.SetParent(ChoiceButtonHolder.transform);
}
This is my code
You code is seems okay, i used it in empty project, and all is perfectly works.
But I suspect you have a error on the ChoiceButtonHolder . Lets clarify that ChoiceButtonHolder have to be a GameObject in the scene. It seems that your ChoiceButtonHolder is also a prefab like s1Button_prefab. You can’t set PREFAB as the parent. Parent have to be a GameObject in the scene. If ChoiceButtonHolder already in your scene you can find it by name or tag, and use it as a parent.
Ok, so I have to tag it
How do i have to search it by tag or name?
For this type questions my advice just type in google :
“how to find gameobject by tag in unity”
“how to find gameobject by name in unity”
You will find much more information from forums and Unity Answers.
Simple way of finding by tag is :
ChoiceButtonHolder = GameObject.FindWithTag("TagNameOfChoiceButtonHolder");
Simple way of finding by name is :
ChoiceButtonHolder = GameObject.Find("ChoiceButtonHolderNameOnUnityHierarchy");
Unity Docs: Unity - Scripting API: GameObject.Find
Thank you so much!
I have another question because another scripts has to start a function in the other script but I get this error: "
NullReferenceException: Object reference not set to an instance of an object
NewSpellManager.NewSpellFunction () (at Assets/NewSpellManager.cs:53)"
This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewSpellManager : MonoBehaviour {
private int NewSpell;
[HideInInspector]
public int RealNewSpell;
public Account Account;
private GameObject ChoiceSpawner;
private void Start()
{
NewSpellFunction();
ChoiceSpawner = GameObject.FindWithTag("ChoiceButtonHolder");
}
private void NewSpellFunction()
{
NewSpell = Random.Range(1, Account.HowManySpells += 1);
if(NewSpell == 1)
{
RealNewSpell = Account.n1;
}
if (NewSpell == 2)
{
RealNewSpell = Account.n2;
}
if (NewSpell == 3)
{
RealNewSpell = Account.n3;
}
if (NewSpell == 4)
{
RealNewSpell = Account.n4;
}
if (NewSpell == 5)
{
RealNewSpell = Account.n5;
}
Debug.Log(RealNewSpell);
Debug.Log(NewSpell);
ChoiceSpawner.GetComponent<ChoiceSpawner>().Spawner(RealNewSpell);
}
}
In c# your scripts is reading line by line. In Start method you are calling ChoiceSpawner from NewSpellFunction(); (line 53). But you have not Find it yet. So Unity says that “Dude i cant find your ChoiceSpawner” . Firstly find it, and then you can call it when you want.
private void Start()
{
ChoiceSpawner = GameObject.FindWithTag("ChoiceButtonHolder");
NewSpellFunction();
}
Oh omg I am so dumb
I have one last question, if I instantiated that object how can I change the position, rotation and the scale?
Unity has a good Documentation : https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Here you can see the line:
Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
You can set position, rotation and also a parent with instantiate method.
You can scale GameObject by using localScale : https://docs.unity3d.com/ScriptReference/Transform-localScale.html
void Example()
{
// Widen the object by 0.1
s1Button.transform.localScale += new Vector3(0.1F, 0, 0);
}
Thank you