Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ReggieBeRetro · Jul 11, 2016 at 12:03 AM · scripting problemraycastoptimizationenable and disable script

Disable a script when its not hit by raycast. still uses 17 to 20% in profiler

hello

I am trying to optimize my scripts with mild success. I have disable 2 ui text objects that are included in a object that can be picked up which contains a "facetexttocamera" script that i wrote to face text to the camera of a pickup-able object. Just to give the indication that it can be picked up when a raycast hits the object.

When i look in the profiler it can take up to 15 to 20% at times and frames will not go over 30. Why is the script taking up so much if the objects that holds the script is disabled before before there is a confirmed raycast hit? If i can get this to work i should be around 50 to 60 fps which is acceptable for me.

The 2 text ui objects are set to false when a raycast does not hit the object.

the script is fairly big but a example of the disablement can be found on line 39. also when i look in the hierarchy the text objects are not shaded out so my guess is the .enable and .disable lines are not working but i get no errors.

Below is a the script to check the raycast for you to see how i am disabling the text objects.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class RayCastChop : MonoBehaviour {
     
 
 
     int rayLength = 4;
 
     private TreeCont treeScript;
     private StoneCont stoneScript;
     private BushCont bushScript;
     public ObjectTextDisplay ObjScript;
     public FaceTextToCamera ObjTextScript;
     public string objname;
     private PlayerControls playerAnim;
     public Vector3 fwd;
     public bool ItemResult;
     public GameObject ObjToPickup;
     public ObjectTextDisplay LastPickupItem;
     public Text objText;
     public Text objText2;
 
 
     void  Update (){
 
         RaycastHit hit = new RaycastHit();
         fwd = transform.TransformDirection(Vector3. forward);
 
         if (Physics.Raycast (transform.position, fwd, out hit, rayLength)) {
             
             objname = hit.collider.gameObject.name;
             if (hit.collider.gameObject.tag == "Tree") {
                 ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                 treeScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<TreeCont> ();
 
                 if (ObjScript.transform.GetChild (0).gameObject.name == "Canvas") {
                     objText = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (0).gameObject.name).GetComponent<Text> ();
                     objText2 = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (1).gameObject.name).GetComponent<Text> ();
                 } else {
                     objText = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (0).gameObject.name).GetComponent<Text> ();
                     objText2 = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (1).gameObject.name).GetComponent<Text> ();
 
                 }
 
                 objText.enabled = true;
                 objText2.enabled = true;
                 treeScript.enabled = true;
                 ObjScript.enabled = true;
                 ObjScript.displayInfo = true;
 
                 //StartCoroutine(ObjCoroutine());
                 if (Input.GetMouseButtonDown (0)) {
 
 
 
                     treeScript.treeHealth -= 1;
 
                 }
 
             } else {
                 if (hit.collider.gameObject.tag == "Tree") {
                 ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                 treeScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<TreeCont> ();
                     if (ObjScript.transform.GetChild (0).gameObject.name == "Canvas") {
                         objText = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (0).gameObject.name).GetComponent<Text> ();
                         objText2 = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (1).gameObject.name).GetComponent<Text> ();
                     } else {
                         objText = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (0).gameObject.name).GetComponent<Text> ();
                         objText2 = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (1).gameObject.name).GetComponent<Text> ();
 
                     }        
 
                     objText.enabled = false;
                     objText2.enabled = false;
                     ObjScript.displayInfo = false;
                     ObjScript.enabled = false;
                 treeScript.enabled = false;
                 }
             }
 
             if (hit.collider.gameObject.tag == "Stone") {
                 stoneScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<StoneCont> ();
                 ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                 if (ObjScript.transform.GetChild (0).gameObject.name == "Canvas") {
                     objText = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (0).gameObject.name).GetComponent<Text> ();
                     objText2 = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (1).gameObject.name).GetComponent<Text> ();
                 } else {
                     objText = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (0).gameObject.name).GetComponent<Text> ();
                     objText2 = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (1).gameObject.name).GetComponent<Text> ();
 
                 }
 
                 objText.enabled = true;
                 objText2.enabled = true;
                 stoneScript.enabled = true;
                 ObjScript.enabled = true;
 
                 if (Input.GetMouseButtonDown (0)) {
 
 
 
                     stoneScript.stoneHealth -= 1;
 
                 }
 
             } else {
                 if (hit.collider.gameObject.tag == "Stone") {
                 stoneScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<StoneCont> ();
                 ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                     if (ObjScript.transform.GetChild (0).gameObject.name == "Canvas") {
                         objText = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (0).gameObject.name).GetComponent<Text> ();
                         objText2 = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (1).gameObject.name).GetComponent<Text> ();
                     } else {
                         objText = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (0).gameObject.name).GetComponent<Text> ();
                         objText2 = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (1).gameObject.name).GetComponent<Text> ();
 
                     }
 
                     objText.enabled = false;
                     objText2.enabled = false;
                     ObjScript.displayInfo = false;
                     ObjScript.enabled = false;
                 stoneScript.enabled = false;
                 }
             }
             if (hit.collider.gameObject.tag == "Bush") {
                 bushScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<BushCont> ();
                 ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                 if (ObjScript.transform.GetChild (0).gameObject.name == "Canvas") {
                     objText = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (0).gameObject.name).GetComponent<Text> ();
                     objText2 = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (1).gameObject.name).GetComponent<Text> ();
                 } else {
                     objText = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (0).gameObject.name).GetComponent<Text> ();
                     objText2 = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (1).gameObject.name).GetComponent<Text> ();
 
                 }        
 
                 objText.enabled = true;
                 objText2.enabled = true;
                 ObjScript.enabled = true;
                 bushScript.enabled = true;
 
 
                 if (Input.GetMouseButtonDown (0)) {
 
 
 
                     bushScript.bushHealth -= 1;
 
                 }
 
             } else {
                 if (hit.collider.gameObject.tag == "Bush") {
                 bushScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<BushCont> ();
                 ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                     if (ObjScript.transform.GetChild (0).gameObject.name == "Canvas") {
                         objText = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (0).gameObject.name).GetComponent<Text> ();
                         objText2 = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (1).gameObject.name).GetComponent<Text> ();
                     } else {
                         objText = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (0).gameObject.name).GetComponent<Text> ();
                         objText2 = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (1).gameObject.name).GetComponent<Text> ();
 
                     }
 
                     objText.enabled = false;
                     objText2.enabled = false;
                     ObjScript.displayInfo = false;
                     ObjScript.enabled = false;
                 bushScript.enabled = false;
                 }
             }
 
             if (hit.collider.gameObject.CompareTag ("Pickup")) {
                 
 
                 ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                 LastPickupItem = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                 if (ObjScript.transform.GetChild (0).gameObject.name == "Canvas") {
                     objText = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (0).gameObject.name).GetComponent<Text> ();
                     objText2 = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (1).gameObject.name).GetComponent<Text> ();
                 } else {
                     objText = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (0).gameObject.name).GetComponent<Text> ();
                     objText2 = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (1).gameObject.name).GetComponent<Text> ();
 
                 }    
 
                 objText.enabled = true;
                 objText2.enabled = true;
 
                 ObjScript.enabled = true;
                 ObjScript.displayInfo = true;
             
                 ObjToPickup = hit.collider.gameObject; 
                 ItemResult = hit.collider.gameObject.CompareTag ("Pickup");
             
             } else {
                 
                 //ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
                 if (ItemResult) {
                     ObjScript = GameObject.Find (hit.collider.gameObject.name).GetComponent<ObjectTextDisplay> ();
 
                     if (ObjScript.transform.GetChild (0).gameObject.name == "Canvas") {
                         objText = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (0).gameObject.name).GetComponent<Text> ();
                         objText2 = GameObject.Find (ObjScript.transform.GetChild (0).GetChild (1).gameObject.name).GetComponent<Text> ();
                     } else {
                         objText = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (0).gameObject.name).GetComponent<Text> ();
                         objText2 = GameObject.Find (ObjScript.transform.GetChild (1).GetChild (1).gameObject.name).GetComponent<Text> ();
 
                     }
 
                     objText.enabled = false;
                     objText2.enabled = false;
 
                     LastPickupItem.displayInfo = false;
                     ObjToPickup = null; 
                     ItemResult = false;
                     ObjScript.enabled = false;
                 }
 
 
 
             }
 
         } 
     }
 
 }

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

0 Replies

· Add your reply
  • Sort: 

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

86 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

Related Questions

A more optimized way to adjust duplicate integer values In a list? 0 Answers

i need help with raycast 2 Answers

Shoot towards mouse except mouse y 1 Answer

Scripting Problems with var arrays 1 Answer

RayCast issues 0 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