Can you call script from another script ?
what i really need is when the object is on the scene it well call another script
like
public int Numper; public Script ItemsScript;
i want if you started the game it well do it like void Start () { Script = player } how can i do this
i know how to do it in javascript but i need it on c#
like this function Start () { Find the OtherScript which is attached to any game object in the scene. var other : OtherScript = FindObjectOfType(OtherScript); other.DoSomething(); }
Answer by Joey_pc · Nov 09, 2015 at 03:50 PM
if its the game object use
Script = GameObject.Find("theObjectName").GetComponent(); Make sure you have no typo in the name! Also make sure to use not a name of script and name to access the script, use lowercase:
private Gold gold; also you have a typo in the getcomponent! read my first answer again! Sorry my bad! I fixed my prob.
Answer by OctoMan · Nov 09, 2015 at 03:17 PM
private ItemsScript Script;
void Start()
{
Script = GameObject.FindwithTag("theObjectwithTheScript").GetComponent<ItemsScript>();
//or
Script = GameObject.Find("theObjectName").GetComponent<ItemsScript>();
Script.DoWhatever(); }
Is the gameobject you try to access tagged with that or is it the name for the game object?
"FirstPersonCharacter" i mean.
Your answer
![](https://koobas.hobune.stream/wayback/20220612044456im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to find all GameObjects in Hierarchy by name and put them in an Array[] ? (C#) 1 Answer
GameObject.Find not working? 1 Answer
How can I find the largest GameObject in an array? 0 Answers
Method not working inside FixedUpdate 2 Answers
Referencing variable from another script on another object 3 Answers