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
0
Question by annonymushubh · Jul 08, 2017 at 02:34 PM · unity 5multiplayerphoton

how i instantiate a object make a prefabs at runtime i dont have to save in folder any thing..

I have to convert the single player to multiplayer game so in multiplayer to have player object to spawn to the other devices but i have Ladder and snake game in this don't have the player prefabs because the player is generated while playing the game so how can grab the player run time and instantiate to the other screens. the hierarchy is like this holding the player object is like this...alt text

file.jpg (26.8 kB)
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 UnityCoach · Jul 08, 2017 at 03:57 PM

Prefabs are GameObjects, simply stored as assets.

If you want to instantiate a GameObject that exists in the scene, you have to find it (by name, by tag, by component) and then Instantiate it like you do with a Prefab reference.

 Player player = FindObjectOfType<Player>(); // finding the player by component
 GameObject player2 = (GameObject)Instantiate (player.gameObject); // instantiate a new copy

 GameObject player = GameObject.FindGameObjectWithTag ("Player"); // finding the player by tag
 GameObject player2 = (GameObject)Instantiate (player); // instantiate a new copy

 GameObject player = GameObject.Find ("Player"); // finding the player by name
 GameObject player2 = (GameObject)Instantiate (player); // instantiate a new copy
Comment
Add comment · Show 1 · 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 annonymushubh · Jul 16, 2017 at 06:04 PM 0
Share

i confused totally please explain briefly

avatar image
0

Answer by epicpython · Jul 16, 2017 at 07:42 PM

@annonymushubh You can't make prefabs at runtime. Prefabs must be stored in a folder, before runtime.

If you want to keep data (such as data about the player) while switching scenes, store it in an object, and put:

public void Awake() { DontDestroyOnLoad(this); }

Here is an example from some of my code for Immune System TD. I load a new scene for each level, but I want to keep the information about which level the player is on, after I move to the new scene.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class retaindata : MonoBehaviour {

 public int LevelNum = 1;//the current level the player is playing

 public int getLevelNum()
 {
     return LevelNum;
 }
 public void increaseLevelNum()
 {
     LevelNum += 1;
 }
 public void resetLevelNum()
 {
     LevelNum = 1;//reset to level one
 }

 public void Awake()
 {
     DontDestroyOnLoad(this);
 }
     

}

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

163 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 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

How to display player coins on multiplayer 1 Answer

How to change colors in unity using photon? 0 Answers

Light.color Sync ? 1 Answer

How to connect to our own dedicated server using photon networking in unity?(Self-hosted) 0 Answers

Player Smoothness across network (PUN) 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