Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 JackoMiG · Aug 13, 2014 at 06:40 AM · javascriptinstantiateprefabvariableammo

[Urgent]Changing variables of an object AFTER instantiation

Hello,

I am trying to get ammo pickups to work so that when you walk through the collider 30 is added on to the reserve ammo.

The problem is the weapon and its script for the weapon (which includes the variable about the ammo) are instantiated once the scene starts so I don't know how to assign the count which it is adding to. I'm fairly new on coding, here is some of my scripts involved. I have tried putting in a prefab of the weapon in the scene and referenced that but it didn't work. Please help I am doing this for a school assignment and it is due soon.

Player Stats Script

 #pragma strict

 var MaxHealth = 100;
 var Health : int;
 var aug : GameObject;//referencing the weapon
 
 aug = GameObject.FindWithTag("Aug1T 1");//referencing the weapon
 
 function Start ()
 {
     Health = MaxHealth;
 }
 
 function ApplyDamage (TheDamage : int)
 {
     Health -= TheDamage;
     
     if(Health <= 0)
     {
         Dead();
     }
 }
 
 function Dead()
 {
     //RespawnMenu.playerIsDead = true; 
     Debug.Log("Player Died");
 }
 
 function RespawnStats ()
 {
     Health = MaxHealth;
 }
 
 function OnTriggerEnter(other : Collider)
 {
     if(other.tag == "Ammo")
     {
         GameObject.Find("Aug1T 1").GetComponent(Weapon_Base).reserveAmmo += 30; //add 30 to the reserve ammo on collision
         Destroy(other.gameObject);
     }
 }

Weapon_Base Script

 var reserveAmmo : int = 60;            //how many bullets does this weapon have, not including those in the clip?
 var clipMax : int = 30;                    //how many bullets is this weapon's clip cabable of holding, max?
 var clipAmmo : int = 30;                //how many bullest are actually in this weapon's clip?
 var damageValue : float = 10;        //how much damage does this weapon do?

alt text

a1g.jpg (51.4 kB)
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

2 Replies

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

Answer by JackoMiG · Aug 13, 2014 at 07:09 AM

I figured it out! The instantiated (clone) object needed to referenced! Here's what I changed in case anyone was having the same problem.

 var aug : GameObject;//referencing the weapon
  
 aug = GameObject.FindWithTag("Aug1T 1(Clone)");//referencing the weapon

  GameObject.Find("Aug1T 1(Clone)").GetComponent(Weapon_Base).reserveAmmo += 30; //add 30 to the reserve ammo on collision

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 konsnos · Aug 13, 2014 at 08:37 AM 0
Share

It would be best to keep your GameObjects and Components referenced in code. Set a variable on Start and keep it. If you do what you did in runtime each time, it would hit performance.

avatar image JackoMiG · Aug 14, 2014 at 07:20 AM 0
Share

Yer I ended up moving all the code to the weapon code so that the ammo GUI would update immediately.

avatar image
1

Answer by Dolzen · Aug 13, 2014 at 08:14 AM

This should work.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
 
     public int MaxHealth = 100;
     public int Health;
     public Transform aug; 
 
     // Never use FindWithTag or Find if you don't need it since
     // it uses a lot of resources, click and drag the weapon from 
     // the proyect view to the inspector of the game object wich haves the PlayerScript instead.
 
     // Use this for initialization
     void Start () {
         Health = MaxHealth;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void ApplyDamage (int TheDamage)
     {
         Health -= TheDamage;
         
         if(Health <= 0)
         {
             Dead();
         }
     }
     
     void Dead()
     {
         //RespawnMenu.playerIsDead = true; 
         Debug.Log("Player Died");
     }
     
     void RespawnStats ()
     {
         Health = MaxHealth;
     }
     
     void OnTriggerEnter(Collider other)
     {
         if(other.tag == "Ammo")
         {
             aug.GetComponent<Weapon_Base>().reserveAmmo += 30; //add 30 to the reserve ammo on collision
             Destroy(other.gameObject);
         }
     }
 }

NOTE: In order to OnTriggerEnter works, the aug gameobject must have a collider, and one of the object must have a riggibody atached, also the collider must have the IsTrigger box checked, if you don't want to use OnTriggerEnter, better use OnControllerColliderHit:

 function OnControllerColliderHit(ControllerColliderHit hit)
 {
     if(hit.collider.tag == "Ammo")
         {
             aug.GetComponent<Weapon_Base>().reserveAmmo += 30; //add 30 to the reserve ammo on collision
             Destroy(aug.gameObject);
         }
 }
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

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

Prevent instantiating on collider 1 Answer

Instantiate a prefab with a key press to a relative location? 1 Answer

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

How to access a variable from a game object once instantiated (C#) 1 Answer

Cannot assign prefab in Inspector? 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