Setting parent through custom script not working.
I'm trying to attach a parent to my game block, however this is not working in one case. I have my own class called GameBlock that is derived from a MonoBehavior. When I'm trying to set parent for this object like in this example:
GameObject go = Instantiate(....) as GameObject;
GameBlock block = go.GetComponent<GameBlock>();
// This is not working
block.transform.SetParent(someParent);
//This works well
go.transform.SetParent(someParent);
Is there a way how to SetParent when a have reference to my custom object?
You can't "reparent" a component. You can only reparent a GameObject in the same way you can do it in the hierarchy window of the editor.
If that's really what you want to do, you have to remove the component from one GameObject and attach a new one to the other GameObject,
But is that really what you want to do?
I just wanted to set the parent for the GameObject the component is attached to. $$anonymous$$aybe something like
block.gameObject.transform.SetParent(parent);
but this is also not working.
But you already have the GameObject.
This alone should do it:
//This works well
go.transform.SetParent(someParent);