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 /
This question was closed Jan 24, 2015 at 06:25 PM by Ujean917 for the following reason:

Unattentiveness to coding.

avatar image
0
Question by Ujean917 · Jan 24, 2015 at 03:20 AM · c#sendmessagereceiver

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.

Comment
Add comment · Show 2
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 SilentSin · Jan 24, 2015 at 03:53 AM 0
Share

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?

avatar image Ujean917 · Jan 24, 2015 at 04:49 AM 0
Share

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 alt text

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. alt text

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.

untitled2.png (2.8 kB)
untitled.png (5.9 kB)

1 Reply

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · 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 Ujean917 · Jan 24, 2015 at 05:53 PM 0
Share

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

Answers Answers and Comments

19 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

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


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