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 Reddwarff · Jul 17, 2020 at 01:12 AM · scripting problemvariabletextmesh

Variables keep getting swapped with the other.

I have two TMPro's in my scene and my goal with them is to display variables gotten from objects that get caught within a raycast projected based on the Cursor's position.

The problem I keep having is upon running the script and getting the variable info from an object, one of the public TMPro variable gets switched out to the other one, while the second one stays the same.

Here is the code:

 using UnityEngine;
 using TMPro;
  
 public class raycastGetVariables : MonoBehaviour
 {
     public TextMeshPro healthText;
     public TextMeshPro nameText;
     public float damage = 0f;
     public Camera cam; // Get a public reference to the camera.
  
     private void Update()
     {
         healthText = FindObjectOfType<TextMeshPro>(); // Finds TMPro object in scene.
         nameText = FindObjectOfType<TextMeshPro>();
         Ray ray = cam.ScreenPointToRay(Input.mousePosition); // Allows the ray to be projected from the camera and it's position to be the mouse's position.
         RaycastHit hitResult; // Store the hit result.
  
         if (Input.GetButtonDown("Fire1")) // Fire1 is mapped to the left mouse button.
         {
             print("CLICK!"); // To ensure that the click registered. Gotta be sure.
             if (Physics.Raycast(ray, out hitResult)) // Checks if the raycast hit something.
             {
                 if (hitResult.transform.gameObject.tag == "Enemy") // Will check if the collider has the tag "Enemy".
                 {
                     var target = hitResult.transform.gameObject; // Store the hit result transform and GameObject to a variable.
                     variableContainer container = target.GetComponent<variableContainer>(); // Gets the script component in which the variables are stored.
                     string nameVar = container.Name; // Store name var into a string var.
                     float healthVar = container.Health; // Store health var into a foat var.
  
                     nameText.text = "Name: " + nameVar; // Update the nameText to display the name.
                     healthText.text = "Health: " + healthVar.ToString(); // Update the healthText to display the health, and converted the float to string.
                 }
             }
         }
     }
 }


I believe it has something to do with FindObjectOfType, but I don't know what. Is there an alternate way to do this? or a more efficient way of doing this? If there is a way to fix it, please tell me. Thank You! I should add that I am a noobie to unity, so I understand if one finds the code atrocious.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Jul 17, 2020 at 07:27 AM

Remove the lines that assign to the healthText and nameText variables and assign them manually in the inspector. FindObjectOfType will just return the first object it finds - you use it twice (both times it will find the same component as nothing has changed in the scene) and thus assign the same object to both variables.

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

238 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 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 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 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 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 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 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 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 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 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 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 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

I am trying to access a variable from another script. 1 Answer

Access a variable from another script in update function 1 Answer

Passing variables' values between two or more scripts 1 Answer

Variables modified on other scripts through a Editor Script reset on Play? 1 Answer

How do I make a variable of type C# class? 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