• 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 Voyder_Rozann · Feb 18, 2017 at 08:40 AM · c#instantiateprefabscript.variable

Instantiate a script into an instantiated prefab

Hello, I have a prefab called 'Enemy' and I wanted to instantiate it with an existing script as a new script for new variables ! Like instantiate the script into the instance of the 'Enemy' to acces new variables !

I tried to found the answer but that takes me one day of time...! I code in C#

Thanks to everyone in advance ! :)

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

3 Replies

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

Answer by Voyder_Rozann · Feb 18, 2017 at 05:40 PM

Thanks to @UnityCoach for helping me through t$$anonymous$$s !!!

Here's my 2 scripts :

Bullet Script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bullet : MonoBehaviour
 {
     public float damage = 0.000092f;
     private static int numberOfEnemyCollisions;
 
     // use OnEnable to reset values, if you come to use some Object Pooling as Awake and Start will only ba called once
     private void OnEnable()
     {
         damage = 0.000092f;
         numberOfEnemyCollisions = 0;
     }
 
     public void OnTriggerEnter2D(Collider2D Col)
     {
         if (Col.tag == "Enemy" || Col.tag == "Enemy2")
         {
             numberOfEnemyCollisions += 1;
 
                 Col.gameObject.SendMessage("ApplyDamage", damage, SendMessageOptions.RequireReceiver); // send a message to the enemy gameobject that has an ApplyDamage (float damage) method
                 damage /= 2.25f; // divide the damage every time you $$anonymous$$t
             
             if (numberOfEnemyCollisions == BulletPenetration.NumOfCol)
             {
                 Destroy(gameObject, 0.1f);
                 numberOfEnemyCollisions = 0;
             }
  // destroy the bullet if its damage has come under a given threshold
         }
 
         if (Col.tag == "Wall")
         {
             Destroy(gameObject);
         }
     }
 
     IEnumerator SetTo0()
     {
         yield return new WaitForSeconds(1);
     }
 }


Enemy script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Enemy : MonoBehaviour
 {
     public float health = 0.0003f;
     public Slider slidemyins;
     public float EnemyLifeMAX;
 
     // damage property to handle damage and destruction
     private float _damage;
     public float Damage
     {
         get { return _damage; }
         set
         {
             if (value != _damage)
             {
                 _damage = value;
 
                 if (_damage > health)
                     Destroy(gameObject); // destroy when damage reaches a critical level
             }
         }
     }
 
     // messages can be private, just like other builtin messages like Start and Update
     private void ApplyDamage(float damage)
     {
         Damage += damage;
     }
 
     private void Start()
     {
         slidemyins = GetComponentInC$$anonymous$$ldren<Slider>();
         gameObject.GetComponent<Enemy>().EnemyLifeMAX = 0.0003f;
         gameObject.GetComponent<Enemy>().health = 0.0003f;
     }
 
     private void Update()
     {
         slidemyins.GetComponent<Slider>().maxValue = EnemyLifeMAX;
         slidemyins.GetComponent<Slider>().value = _damage;
     }
 }
 

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

Answer by UnityCoach · Feb 18, 2017 at 09:17 AM

If you instantiated an object that already has the script, you can get it with GetComponent. Like :

 public Stuff InstantiateNewStuff (Stuff stuff)
 {
     GameObject g = Instanciate (stuff.gameObject);
     return g.GetComponent<Stuff>();
 }

If the game object doesn't have it, you can simply add it :

 GameObject newGameObject = Instanciate (prefabReference);
 newGameObject.AddComponent<Stuff>();
Comment
Add comment · Show 4 · 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 Voyder_Rozann · Feb 18, 2017 at 12:15 PM 0
Share
avatar image UnityCoach Voyder_Rozann · Feb 18, 2017 at 01:12 PM 1
Share
avatar image UnityCoach Voyder_Rozann · Feb 18, 2017 at 03:22 PM 1
Share
avatar image Voyder_Rozann UnityCoach · Feb 18, 2017 at 05:33 PM 0
Share
avatar image
0

Answer by Voyder_Rozann · Feb 18, 2017 at 01:37 PM

@UnityCoach In t$$anonymous$$s case, i want my bullet 'penetration' to, everytime OnTriggerEnter2D collide an enemy, divide the Damage do to the enemy by 2 every next enemy the bullet collide ! But when I shoot, the bullet appear, the bullet do damage, But right next, I shoot the second bullet that takes the count of my first bullet

Here's my Bullet script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Shoot2 : MonoBehaviour
 {
     public static float ColliderNum;
     public float ActualCount;
     public static float DivDamage;
     private void Start()
 
     {
         DivDamage = 1f;
         ColliderNum = 0f;
     }
 
     public void OnTriggerEnter2D(Collider2D Col)
     {
         if (Col.tag == "Enemy" || Col.tag == "Enemy2")
         {
             DivDamage += 1f;
             ColliderNum += 1f;
 
             if (ColliderNum == BulletPenetration.NumOfCol)
             {
                 DivDamage = 1f;
                 ColliderNum = 0f;
                 Destroy(gameObject);
             }
         }
 
         if (Col.tag == "Wall")
         {
             DivDamage = 1f;
             ColliderNum = 0f;
             Destroy(gameObject);
         }
     }
 }


Here's my Bullet Penetration Script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class BulletPenetration : MonoBehaviour
 {
     public float PRICE4;
     public float HM4;
     public Text text4;
     public Button bouton4;
     public Color colorgreen;
     public static float NumOfCol;
     
     private void Start()
     {
         NumOfCol = 1f;
 
         PRICE4.ToString("n2");
         colorgreen.g = 0.5f;
         bouton4.image.color = Color.red;
 
         PRICE4 = PlayerPrefs.GetFloat("PRICE4", 0.89f);
         HM4 = PlayerPrefs.GetFloat("HM4", 0f);
     }
 
     private void OnDestroy()
     {
         PlayerPrefs.SetFloat("PRICE4", PRICE4);
         PlayerPrefs.SetFloat("HM4", HM4);
     }
 
     private void Update()
     {
         if (EUROSARGENT.EUROS <= PRICE4)
         {
             bouton4.image.color = Color.red;
         }
         else if (EUROSARGENT.EUROS >= PRICE4)
         {
             bouton4.image.color = colorgreen;
         }
 
         text4.text = ("Bullet P Lenght" + ("\n€=" + PRICE4) + ("\nYou Bought " + HM4));
     }
 
     public void Buy004()
     {
         if (EUROSARGENT.EUROS >= PRICE4)
         {
             EUROSARGENT.EUROS -= PRICE4;
             PRICE4 = Mathf.Round(PRICE4 * 110f) / 100f;
             HM4 += 1f;
             NumOfCol += 1;
         }
     }
 }


And Here's my EnemySpawn script :

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class EnemySpawn : MonoBehaviour
 {
     public Slider slidemyins;
     public float speed;
     public float EnemyLifeMAX;
     public float EnemyLife;
     public float EnemyLifeODD;
     public float DoPlayerDamage;
     public static float DoEnemyDamage;
     public float bulletdamage;
 
     IEnumerator PlayerDamage()
     {
         yield return new WaitForSeconds(0.94f);
         PlayerShoot.PlayerHealth -= DoPlayerDamage;
         yield return PlayerDamage();
     }
 
     private void OnTriggerEnter2D(Collider2D col)
     {
         if (col.tag == "Bullet")
         {
             EnemyLifeODD -= bulletdamage / Shoot2.DivDamage;
         }
 
         if (col.tag == "Player")
         {
             gameObject.GetComponent<EnemySpawn>().speed = 0.0001f;
             StartCoroutine(PlayerDamage());
         }
         else
         {
             StopCoroutine(PlayerDamage());
             gameObject.GetComponent<EnemySpawn>().speed = 50f;
         }
     }
 
     void Start()
     {
         speed = 50f;
         DoPlayerDamage = 0.00001f;
         EnemyLifeMAX = 0.0003f;
         EnemyLife = 0.0003f;
         EnemyLifeODD = 0.0003f;
         slidemyins.GetComponent<Slider>();
     }
 
     void Update()
     {
         bulletdamage = BulletDamage.DEALENEMYDAMAGE;
 
         slidemyins.GetComponent<Slider>().maxValue = EnemyLifeMAX;
         slidemyins.GetComponent<Slider>().value = EnemyLifeODD;
 
         Vector2 Position = transform.position;
         Position = new Vector2(Position.x - speed * Time.deltaTime, Position.y);
         transform.position = Position;
 
         if (gameObject.GetComponent<EnemySpawn>().EnemyLifeODD <= 0)
         {
             gameObject.SetActive(false);
             EUROSARGENT.GetTheMoney();
         }
     }
 }
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 UnityCoach · Feb 18, 2017 at 03:03 PM 1
Share
avatar image Voyder_Rozann UnityCoach · Feb 18, 2017 at 03:08 PM 0
Share

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Instantiate a prefab and assign it's values 1 Answer

Is there a way to check if all children of an object(can be prefeb) are Loaded Properly Unity 3d? 0 Answers

What is Casting in Unity ? 1 Answer

Raycast hit not detection gameobject? 0 Answers

Error: "Use of unassigned local variable 'BoxPrefab'".,"Use of unassigned local variable 'BoxPrefab' 0 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