- Home /
How can i set one gameobject as another in code?
How can i do this:
GameObject a;
GameObject b;
a = b;
I can try to, but it says that it is read-only, how can i get around this?
Answer by robertbu · Jan 01, 2014 at 08:21 AM
There is nothing wrong with you are trying to do. The following compiles:
using UnityEngine;
using System.Collections;
public class Bug25a : MonoBehaviour
{
public GameObject a;
public GameObject b;
void Start () {
GameObject c ;
GameObject d = GameObject.Find("Player");
a = b;
c = d;
}
}
To help you solve your problem, we would need to see the exact code that is generating the error you describe.
I've tried applying your idea to my code, it gives the same error message
Code: public static void RebuildStockPlanet(GameObject gcb) { foreach(CelestialBody body in FlightGlobals.Bodies) { if(body.name == gcb.name) { body.gameObject = GameObject.Find(gcb.name); } } }
You cannot replace the game object your script/component is attached to. What are you needing to do here?
Your answer
Follow this Question
Related Questions
gameObject.find doesn't work 1 Answer
How to disable gameobjects in code? 4 Answers
Set variable in script from a different script... 3 Answers
how to make my gameobjects disable and enable? 2 Answers
Instantiate as child 3 Answers