- Home /
Setting parent causing unknown problems
I am trying to text a text box running, so the player can kinda get a story perspective as they play my game, and so they can know other important stuff. Right now, my script is producing the errors
=The best overloaded method match for UnityEngine.Transform.SetParent(UnityEngine.Transform)' has some invalid arguments =Argument
#1' cannot convert UnityEngine.GameObject' expression to type
UnityEngine.Transform'
I am calling the function form outside of the actual script, not sure that is what is causing the problem at all. The parent object that the text prefab is going into has a grid layout, so it should fall in line(as i guessed) when it works(so the original instantiate location of it should bear no unwanted results as i am guessing)
using UnityEngine;
using System.Collections;
public class TextBox : MonoBehaviour {
public GameObject TextObject;
public GameObject TextArea;
public void AddText(string Words){
//I do not know any other way of temp hiding a gameobject that works, but this works
Instantiate (TextObject, new Vector3 (99999, 99999, 99999), Quaternion.identity);
UnityEngine.UI.Text displayedtext = TextObject.GetComponent<UnityEngine.UI.Text>();
Words = "| " + Words;
displayedtext.text = Words;
TextObject.transform.SetParent (TextArea);
}
}
Answer by Dave-Carlile · Jul 22, 2015 at 02:58 PM
If you go look up the documentation for Transform.SetParent, you'll see that the parameter it expects is of type Transform
. You're passing in a parameter of type GameObject
. This is also what the error message is telling you. Pass in TextArea.transform
instead.
Answer by Cherno · Jul 22, 2015 at 02:59 PM
As evident from the error message, you are trying to pass a GameObject (TextArea) to the SetParent function, but it only accepts a Transform.
TextObject.transform.SetParent (TextArea.transform);
i fixed that one, i feel stupid. But now i get the error of:
[Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.]
which is strange to me(never had this error before)
Usually that means you aren't actually setting the parent to a GameObject
, but rather a Prefab (non-instantiated). You should make sure that you set the parent to an actual instantiated GameObject
; not a Prefab living in a folder somewhere (e.g. Assets/Prefabs/Rock.prefab)
O$$anonymous$$. When I instantiate the object, what do I define "SpawnedText" as, so that it will work correctly? i tried "Object SpawnedText;", but then i cannot setparent.
using UnityEngine;
using System.Collections;
public class TextBox : $$anonymous$$onoBehaviour {
public GameObject TextObject;
public GameObject TextArea;
public void AddText(string Words){
//define SpawnedText here
SpawnedText = Instantiate (TextObject, new Vector3 (99999, 99999, 99999), Quaternion.identity);
UnityEngine.UI.Text displayedtext = SpawnedText.GetComponent<UnityEngine.UI.Text>();
Words = "| " + Words;
displayedtext.text = Words;
SpawnedText.transform.SetParent (TextArea.transform,true);
}
}
Answer by grimofdoom · Jul 23, 2015 at 10:55 AM
Solved it completely, Apperently this works
GameObject NewObject = instantiate(prefab);
NewObject.Transform.SetParent = ParentObject.Transform;
but this did not:
GameObject NewObject = instantiate(prefab, new vector 3 (999,999,999), quaterniun.identity);
NewObject.Transform.SetParent = ParentObject.Transform;