- Home /
[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?
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
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.
Yer I ended up moving all the code to the weapon code so that the ammo GUI would update immediately.
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);
}
}
Your answer
Follow this Question
Related Questions
Change prefab before instantiation 1 Answer
Updating a variable on a script in an instanced object 1 Answer
Putting instantiate inside a variable 2 Answers
How to instantiate a Prefab ? 2 Answers
How to force a variable to prefab 1 Answer