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 jjplay175 · May 11, 2015 at 07:17 PM · c#instantiategetcomponent

instantiate prefab and link 3rd party gameobject script

Hi so basically I have a script that shoots but I want players to have stats and guns to have their own damage

So I have the bullet that is instantiated and the bullet works out the damage it does but I want it to work out the damage using the gun it is shot from and the person that shot it

Hierarchy example

Player1 > Character > Weapons > Assault_Rifle

I have a script on Assault_Rifle

     if (Input.GetButton("Fire1") && Time.time > nextShot && clipBullets > 0 && curReload == false) {
         nextShot = Time.time + gunStats.fireDelay;

         Rigidbody bulletInstance;
         bulletInstance = Instantiate(wepBullet, gunBarrel.transform.position, gunBarrel.transform.rotation) as Rigidbody;
         bulletInstance.AddForce(gunBarrel.transform.forward * bulletSpeed);
         gunBarrel.GetComponent<AudioSource>().Play();
         clipBullets = clipBullets - 1;
         ammoText.text = (clipBullets + " / " + gunStats.clipSize);
     }

script for weapon damage is on Assault_Rifle, script for % bonuses against species is on Character

Comment
Add comment · Show 1
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 xortrox · May 12, 2015 at 10:38 AM 0
Share

Posted as answer ins$$anonymous$$d.

1 Reply

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

Answer by xortrox · May 12, 2015 at 10:39 AM

make a Bullet script that has a public damage variable to set, then after Instantiating the bulletInstance just do:

 Bullet b = bulletInstance.AddComponent();
 b.Damage = 100.0f;//example value

Same concept if you have a "piercing effect"

 b.PiercePercentage = 0.5f;

or simply a damage type enum

 b.DamageType = DamageType.Piercing;

As for the stats part you would have to elaborate on how the stats would work or even be saved (per game?, hiscores? etc).

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 jjplay175 · May 12, 2015 at 04:40 PM 0
Share

Stats are saved through the game like a leveling system, example... Character has a stats script and is referenced in the bullet_Script

 private players_Stats refPlayerStats;

             if (colObjScript.species == "$$anonymous$$echanical") {
                 print ("Is $$anonymous$$echanical");
                 if (refPlayerStats.mechanicalExpert == true) {
                     totalDmg = totalDmg + (totalDmg / 10);
                     colObjScript.curHealth = colObjScript.curHealth - bulletDmg;
                 }

But using your idea... $$anonymous$$aybe it would be better to have the stats on the actual bullet and when the gun instantiates it, it sets the dmg and if the bullet has mechanical dmg bonus...

So ins$$anonymous$$d of the bullet needing to reference everything everytime it spawns, the gun references it and adds it to the bullet

If all the guns are prefabs then I can simply get the object of character by referring to the guns parent? can Instantiate weapons as a child of the character that way

avatar image xortrox · May 12, 2015 at 04:46 PM 0
Share

I assume you would be instantiating the gun as well and this would probably be done from the character object script (or at least how I would do it). As for your damage issue the gun should probably assign the damage for the bullet based on the player having the mechanicalExpert trait yes. Stats should remain on the character object as well I assume.

avatar image xortrox · May 12, 2015 at 04:55 PM 0
Share

my usual ideal setup: Player class instantiates the weapons thus getting their reference for adding components and what not, and usually I pass a reference to the weapon for a "ParentPlayer" variable or just "Player Parent".

Weapon class instantiates the bullets which have specific data like the damage they will deal or even the damage type they would deal (which is deter$$anonymous$$ed by the Parenting player for the weapon. So for instance:

 public class Player : $$anonymous$$onoBehaviour
 {
 
 public float Damage = 10.0f;
 
 public bool Stat$$anonymous$$echanicalExpert = false;
 
 void Start()
 {
 
 //load stats, example with PlayerPrefs
 Stat$$anonymous$$echanicalExpert = (PlayerPrefs.GetInt("SomeUserName.$$anonymous$$echanicalExpert") == 1);
 
 }
 
 void Update()
 {
 
 if(Input.Get$$anonymous$$ouseButtonDown(0))
 {
 
 //pos/rot is based on the weapon barrels exit point
 GameObject bullet = (GameObject)Instantiate(bulletPrefab, position, rotation);
 
 Bullet b = bullet.AddComponent();
 
 foat damage = Damage;
 
 if(Stat$$anonymous$$echanicalExpert)
 {
 damage += (Damage * 0.1f);
 }
 
 b.Damage = damage;
 //here you could also set things like starting speed for the bullet and such depending on the weapon type again
 
 }
 
 }
 
 
 }


avatar image jjplay175 · May 12, 2015 at 05:19 PM 0
Share

Yeh that looks great and the logic of doing it itself will help a lot, some small tweaks and it should work perfectly how I want it

Thanks for the help

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

20 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

Related Questions

How to call a function on an instance 1 Answer

Referencing in a prefab 1 Answer

How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer

Getting instantiated clone's coordinates from the same script 1 Answer

Distribute terrain in zones 3 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