Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by guitarninja · Apr 18, 2013 at 01:53 AM · gameobjectscriptingbasicsreferencelink

How to run function in another script with prefabs? C#

I have gameObject A with script A, how do I then call a function in gameobject b's script b without using the "direct reference" made in the Editor by dragging the gameobject with scriptb onto the "other" variable?

Reason being is that script A's gameobject needs to be a prefab as it will be instantiated and when needed with run a function in scriptb which is my gamecontroller


 using UnityEngine;
 public class ScriptA : MonoBehaviour
 {
   public ScriptB other;
  
   void Update()
   {
     other.DoSomething();
   }
 }



 using UnityEngine;
 public class ScriptB : MonoBehaviour
 {
   public void DoSomething()
   {
      Debug.Log("Hi there");
   }
 }



I've tried all sorts such as the below but just can't get my head around it.

 ScriptB otherScript = GetComponent<ScriptB>();
 otherScript.DoSomething();
 
 ScriptB other = (ScriptB) go.GetComponent(typeof(ScriptB));
 other.DoSomething();



If I have scriptA and scriptb on the same gameobject it just works but putting them on different gameobjects only works using the "direct reference" made in the Editor.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Apr 18, 2013 at 01:53 AM 0
Share
  • http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

  • http://www.youtube.com/watch?v=WPpkGGEtz7$$anonymous$$&t=7m37s

avatar image guitarninja · Apr 18, 2013 at 02:44 AM 0
Share

This is fine for finding gameobjects which strangely enough I can get working. I'm trying to access a script thats on a gameobject so its similar and yet I still have no idea how to do it.

avatar image whebert · Apr 18, 2013 at 02:54 AM 0
Share

FindObjectOfType can be used from any script to find any other script type if it is in the scene.

avatar image Cashews · Jul 11, 2013 at 12:38 AM 0
Share

How would one do this if the GameObject has yet to be instantiated? I'm trying something like this but I keep getting an error "Type "NameofScript" cannot be found"

 public NameofScript other;
 public GameObject go;
  
 void Awake ()
 {
 GameObject go = Instantiate(Prefab, Vector3.zero, Quaternion.identity);
 other = go.GetComponent<NameoftheScript>();
 }
  
 other.function();

Never$$anonymous$$d, it seems like I can't use a C# script to reference a JavaScript. @_@ Is that really too hard to do, Unity?

2 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by guitarninja · Apr 18, 2013 at 04:08 AM

Brilliant I got it working.


 public NameoftheScript other;
 public GameObject SpawnZone;
 
 void Awake ()
     {
         // Setting up the reference.
         GameObject SpawnZone = GameObject.Find("NameoftheGameObject");     
         other = SpawnZone.GetComponent<NameoftheScript >();                 
                     
     }
 
 //just use the below where ever to activate
 other.NameoftheFunction();



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by CrystalRain0329 · Apr 18, 2013 at 04:30 AM

If you can get GameObject.Find working, then you should be able to combine it with your GetComponent example to get what you want:

ScriptB otherScript = GameObject.Find("Name of GameObject with ScriptB you want").GetComponent(); otherScript.DoSomething();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I call a function in another gameObject's script? 5 Answers

Call Functions Across Scripts, Null Object Error 1 Answer

Referencing a Public GameObject from another script. 2 Answers

Accessing Variables within another gameobject's script 2 Answers

How do you reference a GameObject in a C# script that is a part of that GameObject? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges