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 Avi3ator · Dec 26, 2016 at 03:45 PM · unity 5networkingbuildnetworksync

Unet Instantiate

I have this simple building script, where if i press q i can build objects, like a foundation. But this doesnt sync over UNET? How would i make this script sync over unet?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.Networking;
 
 public class BuildingSystem : MonoBehaviour
 {
     public List<buildObjects> objects = new List<buildObjects>();
     public buildObjects currentobject;
     private Vector3 currentpos;
     private Vector3 currentrot;
     public Transform currentpreview;
     public Transform cam;
     public RaycastHit hit;
     public LayerMask layer;
 
     public float offset = 1.0f;
     public float gridSize = 1.0f;
 
     public bool IsBuilding;
 
     void Start()
     {
         ChangeCurrentBuilding(0);
 
         Cursor.visible = false;
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     void Update()
     {
         if(Input.GetKeyDown("q"))
         {
             IsBuilding = true;
         }
         else if(Input.GetKeyDown("e"))
         {
             IsBuilding = false;
         }
 
         if (IsBuilding == false)
         {
             currentpreview.gameObject.SetActive(false);
         }else
         {
             currentpreview.gameObject.SetActive(true);
         }
 
         if (IsBuilding == true)
         {
             startPreview();
         }
         if (Input.GetButtonDown("Fire1"))
         {
             Build();
         }
         if (Input.GetKeyDown("0") || Input.GetKeyDown("1") || Input.GetKeyDown("2"))
         {
             switchCurrentBuilding();
         }
     }
 
     public void switchCurrentBuilding()
     {
         for (int i  = 0; i < 3; i++)
         {
             if(Input.GetKeyDown("" + i))
             {
                 ChangeCurrentBuilding(i);
             }
         }
     }
 
     public void ChangeCurrentBuilding(int cur)
     {
         currentobject = objects[cur];
         if(currentpreview != null)
         {
             Destroy(currentpreview.gameObject);
         }
         GameObject curprev = Instantiate(currentobject.preview, currentpos, Quaternion.Euler(currentrot)) as GameObject;
         currentpreview = curprev.transform;
     }
 
     public void startPreview()
     {
         if (Physics.Raycast(cam.position, cam.forward, out hit, 10, layer))
         {
             if(hit.transform != this.transform)
             {
                 showPreview(hit);
             }
         }
     }
 
     public void showPreview(RaycastHit hit2)
     {
         currentpos = hit2.point;
         currentpos -= Vector3.one * offset;
         currentpos /= gridSize;
         currentpos = new Vector3(Mathf.Round(currentpos.x), Mathf.Round(currentpos.y), Mathf.Round(currentpos.z));
         currentpos *= gridSize;
         currentpos += Vector3.one * offset;
         currentpreview.position = currentpos;
         if (Input.GetButtonDown("Fire2"))
         {
             currentrot += new Vector3(0,90,0);
         }
         currentpreview.localEulerAngles = currentrot;
     }
 
     public void Build()
     {
         PreviewObject PO = currentpreview.GetComponent<PreviewObject>();
         if (PO.IsBuildable)
         {
             Instantiate(currentobject.prefab, currentpos, Quaternion.Euler(currentrot));
         }
     }
 }
 
 [System.Serializable]
 public class buildObjects
 {
     public string name;
     public GameObject prefab;
     public GameObject preview;
     public int Gold;
 }
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Syncing Random vars for NPCs 0 Answers

Can i Call Additive Scene On Network 1 Answer

UNet NetworkServer max connections 4 Answers

Multiplayer Object spawned by client does not show up on host 1 Answer

Multiplayer objects isn't equals. 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