Skip to content

Instantly share code, notes, and snippets.

@Zenneth23
Created October 2, 2013 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zenneth23/6789750 to your computer and use it in GitHub Desktop.
Save Zenneth23/6789750 to your computer and use it in GitHub Desktop.
//Object A needed to ref from ObjectB, with Component C inside it.
//Component C's public float c need to be adjust to 20.0f when made
public class ObjectA: Monobehaviour
{
void Awake()
{
if(Instantiate_GameObject( "ObjectB", "C" ))
{
C tempC = GameObject.Find("ObjectB").GetComponent<C>();
tempC.c = 20.0f;
}
}
bool Instantiate_GameObject( string gameObjectName, string refComponentName )
{
//If the GameObject is not already there, make it
if(GameObject.Find(gameObjectName) == null)
{
GameObject tempGameObject = new GameObject(gameObjectName);
}
//If the GameObject doesnot need Component, ignore
if( refComponentName == "" ) return false;
//If the GameObject doesnot have needed Component, attach it
GameObject refGameObject = GameObject.Find(gameObjectName);
if( refGameObject.GetComponent(refComponentName) == null )
{
refGameObject.AddComponent(refComponentName);
//Return True if it is making new Component, so that additional code can be made to modify newly made Component
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment