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 synec_ · Apr 12, 2015 at 12:49 PM · c#unity 5scriptingbasics

Is there a way I could combine these scripts? If so, how?

Script is toggled with a UI button. If the prefab is on top of the Water element, it should be red/unplaceable. If not, it is green and placeable.

Script 1:

 using UnityEngine;
 using System.Collections;
 
 public class BuildPlanet : MonoBehaviour {
 
 
     public GameObject PlaceVis;
     public GameObject Water;
     Transform PlaceVisMove;
     ForTrigger fortrigger;
     
     
     // Use this for initialization
     void Start () {
         PlaceVis = (GameObject)Instantiate(Resources.Load("temperateoutpost1transparent"));
         PlaceVisMove = PlaceVis.transform;
         fortrigger = Water.GetComponent<ForTrigger>();
     
     }
     
     // Update is called once per frame
     void Update () {
         //re-enables the object if previously disabled
         PlaceVis.SetActive(true);
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             Vector3 Placement = hit.point;
             PlaceVisMove.position = Placement;
             PlaceVisMove.up = hit.normal;
             //transform up is for rotation
             if (!fortrigger.isContact && Input.GetMouseButtonDown(0))
             {
                 Instantiate(Resources.Load("temperateoutpost1"), Placement, PlaceVis.transform.rotation);
                 PlaceVis.SetActive(false);
                 
                 enabled = false;
             }
             
             if (Input.GetMouseButtonDown(1))
             {
                 PlaceVis.SetActive(false);
                 enabled = false;
             }
             if (Input.GetMouseButtonDown(0) && fortrigger.isContact)
             {
                 PlaceVis.SetActive(false);
                 enabled = false;
             }
             
         }
     }
 }



Script 2 (This script was directly on the Water element):

 using UnityEngine;
 using System.Collections;
 
 public class ForTrigger : MonoBehaviour {
 
 
 
     public bool isContact = false;
 
     
     public GameObject UserBuilding;
     public Material Red;
     public Material Green;
     
     // Use this for initialization
     void Start () {
     //finds the moving prefab, and declares it as variable UserBuilding on start
     UserBuilding = GameObject.Find("temperateoutpost1transparent(Clone)");
     //UserBuilding is the transparent prefab moved by mouse
     Material Green = Resources.Load<Material>("Materials/Green");
     Material Red = Resources.Load<Material>("Materials/Red");
     UserBuilding.GetComponent<MeshRenderer>().material = Green;
     
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     void OnTriggerEnter(Collider col)
     {
         UserBuilding = GameObject.Find("temperateoutpost1transparent(Clone)");
         //if in contact, flip isContact so in BuildPlanet.cs you cannot place the prefab in that location
         if (col.gameObject.name == "temperateoutpost1transparent(Clone)")
         {
             isContact = true;
             UserBuilding.GetComponent<MeshRenderer>().material = Red;
         }
     }
         
         
         
     void OnTriggerExit(Collider col)
     {
         //coroutine instead of standard boolean and rend.material.color because C# doesn't like it for some reason
         StartCoroutine("BoolChanger");
     }
     
     IEnumerator BoolChanger()
     {
         UserBuilding = GameObject.Find("temperateoutpost1transparent(Clone)");
         yield return new WaitForSeconds(0.0f);
         isContact = false;
         UserBuilding.GetComponent<MeshRenderer>().material = Green;
     }
 }

C#. Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Spy-King · Apr 12, 2015 at 05:53 PM

If you want both the scripts to function with each other then you could make one of the scripts a required component in the other script like this -

 [RequireComponent(typeof(Script2))]

before the class declaration.

If you actually want to make both the scripts one script then make a new script and add certain conditions to it like a public bool isPlanet and a public bool isWater and when these are true their respective functionalities will work. Then you can put the script in both your objects.

If one object is of type Planet then make the isPlanet bool true so that only the planet functionalities will work.

Hope you understand Sorry if it's long :D

SpyKing

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to display dynamic text which will be recieved from a server like RestAPI on to the AR camera in Vuforia? 0 Answers

Must I attach every script to a gameobject in order to work ? 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 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