Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • 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.

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

Unexpected Symbol? 0 Answers

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

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


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