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 /
avatar image
0
Question by Luxky13 · Feb 03, 2020 at 07:30 PM · findgameobjectswithtag

I'm trying to use find and if statements how do I do this?,Im trying to use if statements with find how do I properly do this?

Currently I have one game object thats being translated and another that's being rotated. My goal is to start rotating said object once the other object has reached a certain location using if and find statements. Below is what I have currently what should I put inside the if statement so that when my translate object reaches a certain location my object starts rotating? Thanks!

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class scale : MonoBehaviour { GameObject Move; public float speed = 5f; Vector3 temp; // Start is called before the first frame update void Start() { Move = GameObject.FindWithTag ("Move"); }

 // Update is called once per frame
 void FixedUpdate()
 {
     if (position.x.Move < -4)
     {
           temp = transform.localScale;
     temp.x += Time.deltaTime;
     transform.localScale = temp;
     }
    
      
 
     
 }

} ,I'm very new to unity and currently, I have one game object being translated and another being rotated. My goal is to start the rotation of the second object once the first has reached a certain position using an if statement. This is what I currently have and I don't know what I'm supposed to put inside the if statement in order to say something like if the position of the object "Move" is <-5 then I can start rotating the other object. Thanks in advance!

public class scale : MonoBehaviour { GameObject Move; public float speed = 5f; Vector3 temp; // Start is called before the first frame update void Start() { Move = GameObject.FindWithTag ("Move"); }

 // Update is called once per frame
 void FixedUpdate()
 {
     if (position.x.Move < -4)
     {
           temp = transform.localScale;
     temp.x += Time.deltaTime;
     transform.localScale = temp;
     }
    
      
 
     
 }

}

Comment
Add comment · Show 1
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 logicandchaos · Feb 04, 2020 at 02:11 AM 1
Share

I would look into C# program$$anonymous$$g more before going on, make a text game to get a good handle on the language, then co$$anonymous$$g back to unity will be easy.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by unity_ek98vnTRplGj8Q · Feb 04, 2020 at 05:23 AM

I agree with the above comment, you do seem to be lacking some serious fundamental understandings about C# itself. Perhaps following some unity tutorials will help. I am, however, a firm believer in just trying things out, so here is the script I would use to rotate an object once the object tagged with "Move" is past a certain point, with plenty (maybe too many) comments to explain why I am doing what I am doing

 //General note on C# convention - classes and functions should be capitalized
 //Variables should not be capitalized
 //While it seems like it doesn't matter now its a really good habit to get into because it makes your code much easier to read
 
 public class Scale : MonoBehaviour {
     
     GameObject move;
     public float speed = 5f;
 
     // Start is called before the first frame update 
     void Start () {
         //Good job calling this in the start function, it is much more efficient than calling it every update frame
         //Make sure you are using the correct function name however - FindWithTag is not what you want
         move = GameObject.FindObjectWithTag ("Move");
     }
 
     // Update is called once per frame
     // Use update here. FixedUpdate is used for physics calculations, and while not technically incorrect will just make it more complicated for you
     void Update () {
         //I have an object called move and I want to read its position
         //In other words I want the transform that belongs to move
         //Then I want the position that belongs to the transform
         //Then I want the x component of that position
 
         //This will set xValue equal to the position along the x axis of the move object
         float xValue = move.transform.position.x;
         //Now we can check if that value is less than our threshold value
         if(xValue < -4){
 
             //localScale contains information about the SIZE of the object
             //if you want to rotate, use rotation
             //in our case, instead of accessing the rotation directly we can use Transform.Rotate
             //lets first figure out how much we want to rotate around each axis
             Vector3 rotationAroundEachAxis = new Vector3(speed*Time.deltaTime, 0, 0);
             //Multiplying speed by Time.deltaTime is good here because we will get a constant speed regardless of the frame rate we are getting
             //This rotation is just a rotation around the x axis. Notice how the y and z values are zero
 
             //Now we just need to feed these values into the function
             //Notice we are calling the Rotate method that belongs to the transform component attached to this game object
             transform.Rotate(rotationAroundEachAxis);
         }
     }
 }


Comment
Add comment · Show 6 · 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 Luxky13 · Feb 04, 2020 at 03:56 PM 0
Share

Thank you so much for putting in the time and effort to help me! However, I'm still having trouble I inputted the find commands you suggested to however my if statement either still triggers no matter what or doesn't trigger at all.

avatar image unity_ek98vnTRplGj8Q Luxky13 · Feb 04, 2020 at 04:38 PM 0
Share

$$anonymous$$aybe some screenshots will help me, can you post some showing your scene and the inspector for both of your objects

avatar image Luxky13 unity_ek98vnTRplGj8Q · Feb 04, 2020 at 07:25 PM 0
Share

Here you go!

screen-shot-2020-02-04-at-115609-am.png (90.2 kB)
Show more comments
Show more comments

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

120 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

Related Questions

GameObjects Array Problem 2 Answers

Resource counter doesn't reset on each iteration 0 Answers

AI cannot find closest Target 0 Answers

Variable only changes for one object that has the script attached and not the other objects with the same script. 1 Answer

So here is some really complicated question. 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