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 falconer · Apr 24, 2013 at 03:13 PM · instantiatenetworkclonecar game

Assigning gameobject to an instantiated prefab through script?

Hello, I'm creating a network multiplayer car game and i'm blocked by this issue of assigning checkpoint transforms to a cloned car. Let me explain it more so as to make it more clear, I have created cube as checkpoints and they are present in the hierarchy as CheckPoint1, CheckPoint2 etc.... Now once i start the game, i need to assign these checkpoints to a script attached to the cloned or instantiated prefab... Any help would be very helpful.

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

Answer by Loius · Apr 24, 2013 at 04:30 PM

GameObject.Find("Checkpoint1") will find an object named Checkpoint1 in the scene. I'm not sure if it checks inside object; you might want to make all your checkpoints the child of "CheckpointFolder" and then use GameObject.Find("CheckpointFolder/Checkpoint"+index).

Comment
Add comment · Show 4 · 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 falconer · Apr 24, 2013 at 04:43 PM 0
Share

well, thanks for that i got the idea but this script uses arrays and i'm just confused about how to assign those checkpoints

 #pragma strict
 
 var checkPointArray : Transform[]; //Checkpoint GameObjects stored as an array
 static var currentCheckpoint : int = 0; //Current checkpoint
 static var currentLap : int = 0; //Current lap
 static var startPos : Vector3; //Starting position
 
 function Start () {
     //Set a simple visual aid for the Checkpoints
     for (objAlpha in checkPointArray) {
         objAlpha.renderer.material.color.a = 0.2;
     }
     checkPointArray[0].renderer.material.color.a = 0.8;
     //Store the starting position of the player
     startPos = transform.position;
 }
 
avatar image Loius · Apr 24, 2013 at 05:35 PM 0
Share

this is basic stuff

 var cp : GameObject;
 var index : int = 0; // whatever your objects start at 
 var checkpoints : List.< Transform > = new List.< Transform >();
 
 do {
   cp = GameObject.Find("Checkpoints/Checkpoint"+index);
   if ( cp == null ) break;
   checkpoints.Add(cp.transform);
 } while ( cp != null )
 
 checkpointArray = checkpoints.ToArray();
avatar image falconer · Apr 24, 2013 at 06:25 PM 0
Share

Well, Im sounding quite noob i suppose, but i'm getting an error saying "The name 'List' does not denote a valid type ('not found'). Did you mean 'UnityEngine.Light'?"

avatar image falconer · Apr 24, 2013 at 06:45 PM 0
Share

corrected the error, but that code doesnt seem to attach those checkpoints to the script on the spawned car. Never $$anonymous$$d thank you

avatar image
0

Answer by Lazy08 · Apr 24, 2013 at 04:13 PM

Let me see if I understand. You have cubes as checkpoints along the racetrack, and need a script(attached to a clone) to get their transforms? Try declaring a public GameObject checkpoint1, checkpoint2; Then, from the editor, select the script on the assets tab. On the top of the Inspector tab, your checkpoints should appear. Assign the cubes to these so that the script will default to them. Then, simply attach the script to the clone car prefab. When instantiated, the clone should have the script attached, and the checkpoints should default to the right cubes.

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 falconer · Apr 24, 2013 at 04:26 PM 0
Share

well, i have a script attached to the clone, but i need to know how to attach these checkpoints to the script on this clone

avatar image Loius · Apr 24, 2013 at 04:27 PM 0
Share

don't put numbers after your variable names; use arrays. :/

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

13 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

Related Questions

Infinite Road Clone Duplication 1 Answer

How to Instantiate and change velocity 2 Answers

How could I save all cloned objects in a scene, and load them back up on start? 1 Answer

Instantiating a GameObject from scene that changes 1 Answer

What can I do to make the objects I instantiated move with the background? 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