Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jul 31, 2014 at 09:47 AM by RedDevil for the following reason:

I solved this on my own

avatar image
0
Question by RedDevil · Jul 28, 2014 at 01:52 PM · prefabgetcomponentinsantiate

Accesing script from newly instantiated game object

Hi guys.So im trying to acces a script form a newly created object.I am creating the player Object at startup and i want enemy object to acces the script from the player object. I was not quite sure how to do this so in the inspector i just put the player prefab. Doing this gave me no errors but the character is not reciving Exp either.

This is the code i have on the enemy script: WarriorObj here is the player prefab but it is not working as i want it too:

  PlayerStats StatsScript = WarriorObj.GetComponent<PlayerStats>();
  StatsScript.Exp += 20;



Comment
Add comment · Show 10
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 Hullu · Jul 28, 2014 at 02:01 PM 0
Share

Do you use Destroy() before this line, assu$$anonymous$$g you use this when enemy dies?

avatar image RedDevil · Jul 28, 2014 at 02:09 PM 0
Share

what im trying to do here is after the enemy has no health left he will then acces the playerstat script and add exp then it will wait and be destroyed after the animation is played and the only thing that does not work is adding the exp and that is because i think it cant acces the WarriorObj variable(since i selected the prefab and not the instantiated player).

avatar image RedDevil · Jul 29, 2014 at 06:10 AM 0
Share

anyone can help?

avatar image prof · Jul 29, 2014 at 06:23 AM 0
Share

Yes, you're right. You need instantiated object and not a prefab GameObject.FindWithTag GameObject.Find

avatar image RedDevil · Jul 29, 2014 at 06:45 AM 0
Share

I just tryed : PlayerObj = GameObject.FindGameObjectWithTag("Player");

And i see in the inspector that it still does not find it.

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by gcoope · Jul 28, 2014 at 02:05 PM

Try creating a public function in PlayerStats that will increment it's Exp. For example:

 public AddExp(int expAmount){
     Exp += expAmount;
 }

Edit: Forgot to add that you should then call this in the same way you did before, but instead of:

 StatsScript.Exp += 15;

Use:

 StatsScript.AddExp(15);



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 RedDevil · Jul 28, 2014 at 02:11 PM 0
Share

im trying to only add exp to the player after the enemy is dead so i need to somehow know when the enemy is dead and i cant just add the enemy in the inspector because im planing to add quite alot of them

Follow this Question

Answers Answers and Comments

26 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

Related Questions

How do I assign a script to an instantiated prefab? 2 Answers

How to Instantiate Custom Class 2 Answers

Disable a GameObjects scripts knowing only the GameObjects name 3 Answers

Why can't I change another script's variable with this script? Thanks, 1 Answer

Unity fire1 prefab in code 2 Answers


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