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

People who like this

0 Show 0
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

  • Sort: 
avatar image

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
epicpython

People who like this

1 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

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

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Player Smoothness across network (PUN) 0 Answers

unity photon 2 not activate child of specific spawned player 0 Answers

Photon realtime gameplay and screen local player 0 Answers

Photon Network Get all the connected players. 0 Answers

Photon network won't join random room with a custom property 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