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 /
avatar image
0
Question by Dustyman567 · Apr 16, 2015 at 03:50 PM · instantiateprefabbuttonhealth

How do you change the int of an object in the scene with an instantiated button?

I am fairly new to unity so forgive me if this is a very easily solved problem. So basically my problem is that you can't apply something from the scene to a slot on a prefab. Since I can't do this I have to change the int (health) on my enemy through its prefab, which I then have no way to switch back to its original value without going into the prefab and changing it back. So what I want to know is how I could link the button to instantiate and specifically have the enemy in the scene as the thing it will subtract health from. here are my current button scripts (does not include the one I use to instantiate it because that script is overly complicated and long and you will probably laugh at me for overcomplicating things.):

script on enemy:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealth : MonoBehaviour{
     
     public int health = 5;
 
     public void remove(int value){
         health -= value;
         
         if(health <= 0){
             health = 0;
             Application.LoadLevel("Lose Screen");
         }   
     } 
 }

script on button:

 using UnityEngine;
 using System.Collections;
 public class ButtonInteraction : MonoBehaviour{
     
     public EnemyHealth _enemyHealth;
     
     public void DamageButton(int damage){
         _enemyHealth.remove(damage);
     }
     
 }

I assume that the answer to this is probably going to be a GetComponent or GameObject.Find thing, but I wouldn't know how to go about stuff like that exactly. Could you help me with this?

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

1 Reply

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

Answer by nug700 · Apr 16, 2015 at 04:32 PM

When you instantiate the prefab, do this:

 GameObject enemy = (GameObject)Instantiate(...);
 EnemyHealth _enemyHealth = enemy.GetComponent<EnemyHealth>();
 
 

That will allow you to call the remove function on the object itself.

Comment
Add comment · Show 2 · 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 Dustyman567 · Apr 16, 2015 at 04:45 PM 0
Share

Omg thank you, it is working now.

avatar image nug700 · Apr 16, 2015 at 05:03 PM 0
Share

Could you mark this as the answer and upvote?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Destroy 1 prefab object and changing the color/image 1 Answer

When I Instantiate a prefab with a button it flies away? 1 Answer

Prefab shooting damage/health? 1 Answer

Is there a way to Instantiate a button prefab as a child of a Canvas? 2 Answers

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 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