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
1
Question by AnishsUsername · Sep 29, 2018 at 07:05 PM · transformif-statementstransform.translate

transform.Translate not working in if-statement?

Hey lovely experts,

I'm currently working on a basic 2D game. I want that when the User clicks a Button, a knife should shoot upwards.

However, my Code doesn't work. I tried a lot, but I don't see any mistake in the code. So it has to be something with the logic. Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawnhandling : MonoBehaviour {
 
     public GameObject knife;
     public GameObject knifePosition;
     public float speed;
     public bool shoot;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetKeyDown("s")){  //just for prototyping
   
 
             spawnKnife();
         }
 
         if(shoot == true){
 
             knife.transform.Translate(0, speed, 0);
         }
         
     }
 
     public void startShooting(){ // Function which will be called by Button 
 
         shoot = true;
     }
 
     public void spawnKnife(){
 
         Instantiate(knife, knifePosition.transform.position, knifePosition.transform.rotation ); 
     }
 }
 
Comment
Add comment · Show 6
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 Stemo688 · Sep 29, 2018 at 07:12 PM 0
Share
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Spawnhandling : $$anonymous$$onoBehaviour {
  
      public GameObject knife;
      public GameObject knifePosition;
      public float speed;
      public bool shoot;
  
      // Use this for initialization
      void Start () {
          
      }
      
      // Update is called once per frame
      void Update () {
  
          if(Input.Get$$anonymous$$eyDown("s")){  //just for prototyping
    
  
              spawn$$anonymous$$nife();
          }
  
          if(shoot == true){
  
              knife.transform.position += new Vector3(0.0F, speed, 0.0F);
          }
          
      }
  
      public void startShooting(){ // Function which will be called by Button 
  
          shoot = true;
          print("shoot");
      }
  
      public void spawn$$anonymous$$nife(){
  
          Instantiate(knife, knifePosition.transform.position, knifePosition.transform.rotation );
          print("spawn");
      }
  }


Try with this code. I added some print() methods to see if you actually call all the methods correctly and I also changed transform.Translate with transform.position +=...

avatar image AnishsUsername Stemo688 · Sep 29, 2018 at 07:17 PM 0
Share

print methods are working, but the knife is still not moving.

avatar image Hellium · Sep 29, 2018 at 07:13 PM 0
Share

Have you tried to put simple Debug.Log to check if the startShooting function is called and the value of shooting in the Update funvtion?

avatar image AnishsUsername Hellium · Sep 29, 2018 at 07:24 PM 0
Share

Yep, the Log says that the functions are called, but somehow the knife still doesn't move.

avatar image hexagonius AnishsUsername · Sep 29, 2018 at 07:39 PM 0
Share

is speed something else than 0 in the inspector?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LCStark · Sep 29, 2018 at 07:43 PM

knife is the prefab you're instantiating. In your Update method you are modifying the transform of the prefab instead of the spawned instance. Add a GameObject field to store reference to your instance:


GameObject knifeInstance;
public void spawnKnife() {
    knifeInstance = Instantiate(knife, knifePosition.transform.position, knifePosition.transform.rotation);
}
Then use that field to change the position:

void Update() {
...
    if (shoot == true) {
        knifeInstance.transform.Translate(0.0f, speed, 0.0f);
    }
}
Comment
Add comment · Show 2 · 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 AnishsUsername · Sep 29, 2018 at 08:10 PM 0
Share

Thank you very much! That solved my problem. I would be really thankful If you could explain why exactly I had to do an instance first?

avatar image LCStark AnishsUsername · Sep 29, 2018 at 08:24 PM 0
Share

When an object is saved as a prefab, that prefab doesn't exist in the scene - it is treated the same way all other assets (like sprites, materials, models, scripts, etc.) are. You have to create an instance of this "template" to have a copy of the prefab in the scene. Your original code was doing the same thing - you used the Instantiate method in your spawn$$anonymous$$nife function. The only thing that was missing was a reference to that instance you were creating. Ins$$anonymous$$d of manipulating the created object, you were changing the transform of the prefab itself.

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

Sprite changing position when button pressed 1 Answer

Vector2.moveTowards but only on one axis 1 Answer

Difference between moving forward with transform.Translate vs transform.localPosition 2 Answers

Change size of an object up to a limit 2 Answers

Make GameObject only move when conditions are fulfilled 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