Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by parjanyaroy · Jun 22, 2013 at 06:28 AM · gameobjectinstantiateprefab

Instantiating a prefab and setting its variables

Hello all , I'm new in unity. Got stuck with a certain problem. I want to create an endless runner platform generation script.The basic logic that i want to implement is that

1.Each platform has a collider frame in the middle. 2.Upon triggering a collision the next platform will get generated having similar collider in the middle. 3.Thus the script aims to keep on generating a new platform as the colliders are hit in successsion.

 #pragma strict
 var prefabPlatform : GameObject = Platform ;
 var testvalue: int =5 ;
 
 
 
 function Start () {}
 
 function Update () {}
 
 function OnTriggerEnter (other : Collider)
 {
 var new_prefab : GameObject;
 
 var childObject=other.gameObject;
 print(other.gameObject.name+" with tag: "+other.gameObject.tag+" has hit the "+gameObject.name);
 if(other.gameObject.tag.Equals("runner"))
 {
 new_prefab=Instantiate(prefabPlatform, Vector3(0, 0, -1*20), Quaternion.identity);
 
 print("instantiating prefab: ");
   }
 else 
 print("Wrong Hit !!!");
 }

Where i am stuck is For the second gameobject that is generated how do i set the prefabPlatform to dynamically get assigned to a random prefab ?

Basically i want to var prefabPlatform : GameObject =(Some Code to set a random prefab from a list of prefabs i have made )

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 KoshX87 · Jun 22, 2013 at 06:54 AM

ok for random generation of platforms you are gonna need to do 2 things. First you need to make every different type of platform you want to have at the moment. You don't need to do them all right now, but probably best if you have 3 for testing purposes. Lets say. one platform is blank, another platform has like a thing you have to jump over(i.e. a hole) and the third platform has something you have to slide under. The functionality of these is up to you, but i'm just saying what each platform does rather than saying platform 1,2 and 3 as it is less confusing.

You know that the empty platform with no enemies or hazards on it needs to be occurring more often than the other platforms. It will be included in the array we are about to make, but i think its important that we understand that this platform will need to happen quite frequently relative to the other platform types.

After you have all the platform types you need. write in the following code:

 var platformArray: Array[];
 var emptyPlatform: GameObject;

// dont worry about this for now var randNumber: int;

Ok. Save this code as a new javascript file and call it "platformGen.js". Make an empty object in your scene and call it "Generator". Drag "platformGen.js" to the "Generator". While you have the generator selected go to the inspector and in the platformArray drop down menu, change its size to the amount of (platforms you want - 1) and then fill each of the empty slots in the inspector with a platform.

Also in the inspector drag the empty platform you have to the emptyPlatform spot.

Now the code below should be placed inside the if(other.gameObject.tag.Equals("Runner"){}:

 randNumber = Random.Range(0, platformArray.Length);
 new_prefab=Instantiate(platformArray[randNumber], Vector3(0, 0, -1*20), Quaternion.identity);


Finally, all that is left to do is code the script such that the instaniate above happens, once and then the empty platform is defintely the next platform. If you don't, the player will be spamming buttons the whole time to stop themselves from dying (haha). To do this you can simply make a boolean variable and when its false, you make a empty platform. After an empty platform is made, the boolean variable can be set to true and so then it will activate the code I have mentioned above.

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
avatar image
0

Answer by parjanyaroy · Jun 22, 2013 at 07:30 AM

thank u :) I'll give it a shot !!

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

16 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

Related Questions

How to instantiate within GameObject 1 Answer

How many time does a prefab takes to load?? 1 Answer

Instantiate as a child on a certain point? 1 Answer

Save gameobjects prefabs from scene A to a list, then instantiate those game objects to another scene. 0 Answers

Referencing gameObject from script after Instantiate 0 Answers


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