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 alexman · Jan 03, 2014 at 09:01 AM · graphics

cannot modify instantiated object

Hi,

I am learning Unity at the moment. I tried to create a slope and create some balls to fall on it and want to see them rolling, however I can't seems to modify the tile's transform, here's my code :

using UnityEngine; using System.Collections;

public class test : MonoBehaviour {

 public Transform stoneBall;
 public Transform tile;
 
 void Start () {

     GameObject newObject = (GameObject)Instantiate (tile);
     newObject.transform.position = new Vector3 (10, 15, 10);
     newObject.transform.Rotate (20f,20f,20f);

 }

 void Update () {

     int x,y,z;
     x = Random.Range (-10, 10);
     y = Random.Range (1, 10);
     z = Random.Range (-10, 10);
     Instantiate(stoneBall, new Vector3 (x, y, z), Quaternion.identity);

 }

}

These two lines doesn't seems to work, if I modify the Prefab's transform rotation through inspector, then i can't see the balls rolling after it land on the tile slope, but I can't seems to make it work using code.

I feel like I missed something very basic?? Thanks.

I can create the slope by using the following code instead, but not using Instantiate :

     // tile
     GameObject tile2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
     tile2.transform.position = new Vector3(0, -3, 0);
     tile2.transform.localScale += new Vector3(20, -0.1f, 10);
     tile2.transform.Rotate(new Vector3(0, 0, 20));

Regards, Alex

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

2 Replies

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

Answer by benni05 · Jan 03, 2014 at 10:03 AM

It is not supposed to work like this.

First change your member variables to

 public GameObject stoneBallPrefab;
 public GameObject tilePrefab;

Add 2 more member vars:

 private Transform stoneBallInstance;
 private Transform tileInstance;

Now instantiate the Prefab and assign the transform of the resulting GameObject to the instance var for tile:

 GameObject newObject = (GameObject)Instantiate (tilePrefab);
 tileInstance = newObject.transform;

So now you have the tileInstance and can reference it wherever you like to assign position, rotation etc.

Next you instantiate the ball - BUT do it in Awake() as well otherwise you will instantiate a ball each frame!

 GameObject stoneBallObject = Instantiate(stoneBall, new Vector3 (x, y, z), Quaternion.identity);
 stoneBallInstance = stoneBallObject.transform;

Assign a position etc. to the stoneBallInstace (which is the transform). Now you can start thinking about what to do in the Update method (moving the transforms etc.). But start with an empty Update and see whether both transforms have been created at their positions.

Before hitting the Play button don't forget to assign proper Prefabs to the Prefab variables (please read the Prefab/Instantiate http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html secion in the docs at least once).

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 alexman · Jan 03, 2014 at 10:48 AM 0
Share

Thanks Benni, that works :)

avatar image benni05 · Jan 03, 2014 at 01:35 PM 0
Share

Ok, great. Feel free to accept my answer as the solution ;-)

avatar image
-1

Answer by Revolving REC0N · Jan 03, 2014 at 10:45 AM

 clone = Instantiate(stoneBall, new Vector3 (x, y, z), Quaternion.identity);
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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Swiping images across the screen - iPhone 0 Answers

How can I make an on-screen speedometer? 2 Answers

Problem with the GUISkin on the script 1 Answer

How to draw sector to 3D circle 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