- Home /
Question by
ken1075240834 · Oct 17, 2019 at 08:26 PM ·
reference-other-object
Get gameobject reference from another script
public class Birb : MonoBehaviour
{
public static GameObject MakeBirbCopy( GameObject original )
{
Birb newBirb = Instantiate(original).GetComponent<Birb>();
newBirb.m_canExecuteAirSpecial = false;
return newBirb.gameObject;
}
}
public class Example: MonoBehaviour
{
}
How do I get Make BirbCopy reference from Birb class to Example class?
Comment
Best Answer
Answer by Ashwinakadaru96 · Oct 17, 2019 at 07:56 PM
Something like this?
public class Birb : MonoBehaviour
{
public static GameObject MakeBirbCopy( GameObject original )
{
Birb newBirb = Instantiate(original).GetComponent<Birb>();
newBirb.m_canExecuteAirSpecial = false;
return newBirb.gameObject;
}
}
public class Example: MonoBehaviour
{
Birb birb = GameObject.Find("Object_with_birb_script").GetComponent<Birb>();
birb.MakeBirbCopy(someGameObject);
}
Answer by lgarczyn · Oct 17, 2019 at 07:53 PM
Not exactly sure what you meant, but here:
public class Example: MonoBehaviour
{
public void RandomFunctionName()
{
Birb birbOriginal = Object.FindObjectOfType<Birb>();
Birb birbClone = Birb.MakeBirbCopy(original);
}
//Or you could do this, if you assign this value in the editor with a prefab or instance
public Birb birbPrefab;
public void RandomFunctionNameAgain()
{
Birb birbClone = Birb.MakeBirbCopy(birbPrefab);
}
}
Your answer

Follow this Question
Related Questions
Match Y axis rotation. 1 Answer
Class hierarchy design and class comumication question. 1 Answer
C#: Create a variable placeholder for an unknown script 3 Answers
Attempting to Increase Ammo by Killing Enemy 1 Answer
I have everything in the right place but why wont my value stay changed ? 2 Answers