- Home /
Unattentiveness to coding.
GameObject not Scaling Correctly after Receiving Message
Hello I am trying to make a game where the player defeats enemies that will increase points accumulated which will be represented by a bar.
Here are two scripts in which I am trying to make this happen:
The first script.
public class EnemyHealth : MonoBehaviour
{
public float maxHealth = 10.0f;
private float currentHealth;
public GameObject deathEffect;
public GameObject damageEffect;
public string collisionTag = "PlayerBullets";
public int theEnergy = 20;
private GameObject secondFormManager;
//public ChangeToSecondForm energyGathered;
void Start ()
{
currentHealth = maxHealth;
secondFormManager = GameObject.FindGameObjectWithTag ("SecondFormManager");
}
void OnTriggerEnter(Collider col)
{
if(col.tag == collisionTag)
{
currentHealth -= 1.0f;
Instantiate(damageEffect,col.ClosestPointOnBounds(transform.position), Quaternion.identity);
if(currentHealth <= 0)
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
//This line of code is the Sender
secondFormManager.SendMessage ("GatherEnergy", 20.0f);
}
}
}
}
The second script.
using UnityEngine; using System.Collections;
public class ChangeToSecondForm : MonoBehaviour
{
private static ChangeToSecondForm instance = null;
public static ChangeToSecondForm Instance
{
get {return instance; }
}
private GameObject playerToBeChangedFrom;
private GameObject playerChangingTo;
public Transform secondFormDisplay;
public float maxEnergy = 100.0f;
public float currentEnergy;
private float energyOriginalXScale;
// Use this for initialization
void Start ()
{
currentEnergy = 0.0f;
energyOriginalXScale = secondFormDisplay.localScale.x;
playerToBeChangedFrom = GameObject.FindGameObjectWithTag ("PlayerCharacter");
playerChangingTo = GameObject.FindGameObjectWithTag ("PlayerCharacterSecondForm");
playerChangingTo.SetActive (false);
}
// Update is called once per frame
void Update ()
{
if (currentEnergy <= 0)
{
secondFormDisplay.GetComponentInChildren<Renderer>().enabled = false;
}
SecondForm ();
}
public void SecondForm()
{
if (Input.GetButtonDown ("TransformToSecond") && (currentEnergy == 100))
{
Debug.Log("Q has been pressed");
currentEnergy = 0;
playerToBeChangedFrom.SetActive (false);
playerChangingTo.SetActive(true);
playerChangingTo.SendMessage ("TransformQueSound", true);
Invoke ("ChangeBack", 10.0f);
}
}
public void ChangeBack ()
{
playerToBeChangedFrom.SetActive (true);
playerChangingTo.SetActive (false);
}
//This body of code is the receiver and is not acting the way I expect it to
public void GatherEnergy(float energyGathered)
{
Debug.LogError ("Got Energy from dead enemy.");
currentEnergy += energyGathered;
if (currentEnergy >= 100)
{
currentEnergy = 100.0f;
}
secondFormDisplay.localScale = new Vector3(energyOriginalXScale * (currentEnergy/maxEnergy), secondFormDisplay.localScale.z, secondFormDisplay.localScale.z);
}
}
If someone can advise me what I am doing wrong and how I should go about something like this next time please do. I want to be able to do this with as little head ache as possible. Thank you in advance.
You haven't told us what's actually happening. What's supposed to scale, how is it supposed to look, and how does it look now?
The game object I selected to be the "secondFormDisplay" is what's going to scale. Its supposed to look like a yellow bar that's filling up. Right now it looks like this
when I run the build but it doesn't scale even after the enemy is killed.
It should look like this as enemies are being defeated and points are being accumulated.
I have it so that it increments at 20 points per enemy killed so it should be 1/5 the length of the pic I showed. But it doesn't do that is the problem.
Answer by SilentSin · Jan 24, 2015 at 04:55 AM
Is the "Got Energy from dead enemy" log showing up?
Also, where you're assigning the new scale, you're using the old Z value for both Y and Z of the new vector.
Try logging each of the values individually to see which ones are going wrong.
The "Got energy from enemy" debug line is showing up. I'll try messing around with those local scale values.
UPDATE: Okay I've figured what the real problem was. I've set the bar responsible of indicating the amount of energy received was set to inactive because I didn't want players to see a sliver of yellow square to distract the player. I've set it to check in the update method that if more than 0 then it would have the setactive enabled.
Anyhow thanks for trying to help me out.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# SendMessage to objects in array 2 Answers
Are singletons in C# faster than SendMessage/GetComponent? 1 Answer
Damage sending with grenades. 1 Answer