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 sachinhosmani · Mar 19, 2016 at 05:24 PM · c#animationmovementproperties

Setting another script's property from code doesn't actually set the property

I am trying to instantiate a cube from a prefab and move it gradually from one position to another.

I have create a game object as a prefab.

Here is how I instantiate my cube:

     using UnityEngine;
     using System.Collections;
     public class CubeScript : MonoBehaviour {
     
         public GameObject cube;
     
         void Start () {
                     // instantiate cube 10 units under transform (of empty parent GameObject which is to whom this script is attached to)
                     GameObject obj = Instantiate (cube, transform.position - new Vector3(0, 10, 0), Quaternion.identity) as GameObject;
                     // Set this as the parent
             obj.transform.parent = transform;
                     // Set end position of the cube
             obj.GetComponent<Movement> ().endPosition = transform.position;
         }
     }

This script to attached to an empty game object which is to serve as a parent to all cubes.

I have attached a "movement" script to the cube prefab.

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
 
     public Vector3 endPosition;
     public float speed;
     void Start () {
            // dummy initialization of end position because we don't have end position yet.
         endPosition = transform.position;
         speed = 5.0f;
     }
 
     void Update () {
                 // move the cube
         transform.position = Vector3.MoveTowards (transform.position, endPosition, speed * Time.deltaTime);
     }
 }


What I notice is that the instantiate cube's end position is never set. It stays the same as the initial position and hence there is no movement noticed.

  1. is there a better / more elegant way of achieving what I am trying to? I am particularly unhappy with how I am having to set the end position property of the instantiated cube after instantiating just so it moves.

  2. why doesn't my code work?

thank you

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
1
Best Answer

Answer by Fredex8 · Mar 20, 2016 at 02:04 AM

I'm not clear on what you are actually trying to do. Where is the end position supposed to be? The cube parent object?

At the moment you are setting endPosition to the cube's parent position in CubeScript:

 obj.GetComponent<Movement>().endPosition = transform.position;

But then overwriting it to the cube's spawn position on start in Movement:

 endPosition = transform.position;

So your cube is just going to be trying to move towards where it already is. Remove endPosition from start and it should work, assuming you want to cubes to move towards the cube parent.

Comment
Add comment · Show 3 · 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 sachinhosmani · Mar 20, 2016 at 06:03 AM 0
Share

oh I didn't realize Start() was going to be called afterwards.

now it works as expected. but is there a more elegant way of doing all this?

thanks!

avatar image Fredex8 sachinhosmani · Mar 20, 2016 at 07:46 AM 0
Share

Well setting variables like endPosition on the instantiated object I think is the best way to set anything which may need to vary between instantiated objects. Though I would probably send a reference to the gameObject ins$$anonymous$$d and get transform.position on the instantiated objects. Only because you might want to check when the cube has collided with that gameObject and then do something to it.

If you had multiple variables you wanted to set on it you could cache the script reference but that's about the only improvement to make.

 $$anonymous$$ovement movementScript = obj.GetComponent<$$anonymous$$ovement>();
 
 movementScript.endPosition = transform.position;

If you're setting several variables on the instantiated object it would be marginally more efficient but it's not needed if you are only setting endPosition. $$anonymous$$ay look a little neater I guess though.

I might choose a different solution to having the target object instantiate cubes 10 units beneath itself but since I don't know what the effect you are going for is I can't really say. Sounds like you're want to create a trail effect on CubeScript or something?

avatar image sachinhosmani Fredex8 · Mar 20, 2016 at 07:53 AM 0
Share

The cubes form a path that a ball has to go over. Their end positions are where they should eventually be, but to make it look cool, I am making them slowly float from underneath. Similarly, after sometime, I will make dead cubes float upwards just before removal/pooling.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make orb walking? 0 Answers

My animation is working but the character won't move. 2 Answers

Stopping an animation when key is unpressed 0 Answers

Switching lanes 1 Answer

I want to stop movement of character while i call animation like Kick Jump Punch etc.. Please help 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