• 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 /
  • Help Room /
avatar image
0
Question by tvashtar · Jun 08, 2019 at 06:05 PM · getcomponentprefabsfunctionscalling

Calling a function in a prefab from a function of another GameObject in C#,Calling a function in a prefab from a function in another GameObject in C#

Hi, I'm a Unity beginner and even though I found several similar questions to mine, none of the answers really helped so I'm asking my specific question:

  • I have a button that instatiates (as child) a prefab GameObject with a script attached.

  • I have another button (also child of the first button) that is then supposed to call a function from that prefab's script.

Here is the code that's on the child button:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class externactive : MonoBehaviour
 {
     public GameObject button;
     public GameObject thing;
     public move myScript;
 
     public void Find ()
     {
         thing = button.transform.Find("Mixfigur(Clone)").gameObject;
         myScript = thing.GetComponent<move>();
         myScript.Activate();
     }
 }

The parent button is drag&dropped into the "button" slot in Unity interface. The prefab's script is called "move" and it has a function "Activate ()". Upon running this, the "thing" slot gets filled with the prefab correctly, but running its function does not work.

I tried the exact same code with a non-prefab game object and it works. The problem seems to be directly tied to it being a prefab. Here is the code for that second example, again on the button that wants to call a function (this time on a non-prefab GameObject):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class zwei : MonoBehaviour {
 
     public GameObject target;
     public ballscript ballFull;
 
     public void Kicking ()
     {
         target = transform.Find("Ball").gameObject;
         ballFull = target.GetComponent<ballscript>();
         ballFull.Kick();
     }
 }

The "Ball" is a child of the button to make sure the same Find operation as in the first script has to find it during runtime. This code works just fine. I cannot see a difference. Does the problem have something to do with prefabs just being handled differently?

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

178 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hello. I want to call a function on another script but a got an error how can I fix this :'( Im a beginner please help. The Script name that I want to call its function is Save Manager. 1 Answer

Unexpected Symbol? 0 Answers

Trying To Spawn a Modified Prefab with NetworkServer.Spawn doesn't work. 0 Answers

Need guidance in multi-use menu accessing prefabs 0 Answers

Im having trouble figuring out a way to make these two functions work. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges