Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Gona · Mar 24, 2010 at 03:39 PM · prefabcomponent

Acces a component on an instantiated object

Well here ou have my problem, I have an instantiated Prefab (BodyPart) by script that appears when i hit the play. This prefab it's instantiated by a Manager script attached to an empty GameObject who has all the game inside. Now, this Prefab (BodyPart) has a script attached call "Follow" as component and it's a C# script similar to "SmoothFollow". I know for sure that this script begins when i instantiate the prefab because y put a Debug.Log on start and works grate. What i need it's to tell this Script who has to Follow, in other words, the Target. But when i'm on Manager script with the instantiated prefab y make GetComponent and then try to access a public function on the script and i can't. HELPE ME PLEEEASE!!

Here you have my code

Manager Script

public class Manager : MonoBehaviour {

void Start () {

prefabTail = GameObject.Instantiate (Resources.Load("BodyPart",typeof(GameObject)))as GameObject;

prefabTail.transform.parent = this.transform;

prefabTail.GetComponent<Follow>();

 prefabTail.FunctionIWantToAcces("Bodypart2");// It says NullReference


}

}

Follow Scrpt

public class Follow : MonoBehaviour { void Start() {

}

public void FunctionIWantToAcces(string targetName) { GameObject targetLoaded = GameObject.Find(targetName); target = targetLoaded.transform;

 }

}

PD: sory if i have english problems i'm From Argentina we speak spanish :p

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 sacredgeometry · Mar 24, 2010 at 06:46 PM 0
Share

are you instantiating multiple objects? or just the one?

avatar image KvanteTore · Mar 24, 2010 at 07:38 PM 0
Share

english protip: access is usually written with a double s :)

avatar image Gona · Mar 27, 2010 at 07:48 PM 0
Share

i'll instantiate multiple objects in the future, but now i just need one

avatar image dissidently · Dec 21, 2010 at 08:59 AM 0
Share

for the love of all that is good, please fix the spelling of acces to access so this comes up trumps in a search. I stumbled on this, but later will need it.

1 Reply

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

Answer by KvanteTore · Mar 24, 2010 at 07:34 PM

It looks like you want to call the FunctionIWantToAcces method on the Follow component, but you are trying to call FunctionIWantToAcces on the Manger component. You should get an instance of the Follow object and call FunctionIWantToAcces on that instance.

void Start () {

 prefabTail = GameObject.Instantiate (Resources.Load("BodyPart",typeof(GameObject)))as GameObject;
 prefabTail.transform.parent = this.transform;

 Follow tailComponent = prefabTail.GetComponent&lt;Follow&gt;();
 tailComponent.FunctionIWantToAcces("Bodypart2");

}

Comment
Add comment · Show 1 · 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 Gona · Mar 27, 2010 at 07:55 PM 0
Share

HEYYY!!! this was perfect thanks!!! i understood how GetComponent works now XD

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

No one has followed this question yet.

Related Questions

Not understanding GetComponent with prefabs 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Determine if an external prefab exists within the project 's assets folder (not in scene) 2 Answers

cloned game object wont call script right... 2 Answers

How to make sprites instantiate based on randomly generated integer? 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