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 09, 2015 at 06:17 PM · c#unity 5transformtrigger

TriggerExit not working as intended/Transforming GameObject Problems

I want to help you guys visualize it. When I push a UI button, the Builder script starts. A green transparent cube follows your mouse to show you where it is going to be placed. If it is in contact with the Water (Blue part in photo) the CanPlace bool goes False, so you cannot place it and the material changes to PlaceRed. When it is no longer in contact with the Water object, the bool goes True, and the if statement for placement can be used. Now whenever it goes into contact with the Water element, it goes red, and stays red, but still can be placed when it is off of the Water element. (also the cube is halfway inside the mesh)

Photos of my project which might be needed are here: http://imgur.com/a/RVjm1 (Imgur because I can only have 2 images on the question)

planetstationtransparent.prefab import settings:

alt text

planetstation.prefab import settings:

alt text

Now it places, but it doesn't place where the transparent cube should be. Builder.cs is what runs when the UI button is pushed and GameObjectDisable is always on.

Script Builder.cs:

     using UnityEngine;
     using UnityEngine.Events;
     using System.Collections;
     
     public class Builder : MonoBehaviour
     {
         GameObject ghostObject;
         Transform ghostObjectTrans;
         GameObjectDisable ghostObjectDisable;
         
         // Use this for initialization
         void Start()
         {
             //grabs the can place script so it cannot be placed on water
             GameObject Water = GameObject.Find("Water");
             // Iwandi : we need to set ghostObjectDisable in the scope of the class to use it in Update
             ghostObjectDisable = Water.AddComponent<GameObjectDisable>();
             //makes translucent object with red and half opacity TODO: CHANGE BACK TO GREEN
             ghostObject = (GameObject)Instantiate(Resources.Load("planetstationtransparent"));
             ghostObjectTrans = ghostObject.transform;
         }
     
         // Update is called once per frame
         void Update()
         {
             //re-enables the object if previously disabled
             ghostObject.SetActive(true);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
     
     
             if (Physics.Raycast(ray, out hit))
             {
                 Vector3 Placement = hit.point;
                 ghostObjectTrans.position = Placement;
                 ghostObjectTrans.up = hit.normal;
                 //transform up is for rotation
     
                 // previous code is for the raycasting of planet mesh
                 // Iwandi : Each statmen whitin a if() has to resolve to true so writing something == true is like writing (something == true) == true
                 if (Input.GetMouseButtonDown(0) && ghostObjectDisable.CanPlace)
                 {
                     GameObject BuildingFinal = (GameObject)Instantiate(Resources.Load("planetstation"));
                     Placement = BuildingFinal.transform.position + ghostObjectTrans.up;
                     
                     enabled = false;
     
                 }
                 if (Input.GetMouseButtonDown(0) && ghostObjectDisable.CanPlace == false)
                 {
                     enabled = true;
     
                 }
                 
                 if (Input.GetMouseButtonDown(1))
                 {
                     ghostObject.SetActive(false);
                     enabled = false;
                 }
             }
         }
     }

 

Script GameObjectDisable.cs:

 using UnityEngine;
 using System.Collections;
 
 public class GameObjectDisable : MonoBehaviour
 {
     [SerializeField]
     Material PlaceRed;
     [SerializeField]
     Material PlaceGreen;
 
     // - we do not use [SerializeField] on canPlace as this is not a value set by the Editor. If you want to debug this value set the Editor/Inspector window to Debug
     bool canPlace;
     
     // - Declaring GhostObject within the scope of the class allows all class Methods to access it as a variable
     GameObject GhostObject;
 
     // - use a public Property with get;set; to control access to local variables
     // - we block external set access to canPlace. this way we know no other script messes with this value
     public bool CanPlace { get { return canPlace; } }
 
     // - Your not allowed to Call GameObject.Find() in a c# Constructor so we call it in Start
     void Start()
     {
         GhostObject = GameObject.Find("planetstationtransparent(Clone)");
     }
 
     void OnTriggerEnter(Collider col)
     {
         //if in contact, change to red and make non placeable
         if (col.gameObject.name == "planetstationtransparent(Clone)")
         {
             // - try to avoid Renderer.material as it can memory leak if you don't know what you're doing.
             GhostObject.GetComponent<Renderer>().material = PlaceRed;
             // - do not redeclare canPlace. We need to set canPlace in the scope of the class to allow accesses later on
             canPlace = false;
         }
     }
 
     void OnTriggerExit(Collider col)
     {
         //if no longer in contact, change back to green and placeable
         if (col.gameObject.name == "planetstationtransparent(Clone)")
         {
             GhostObject.GetComponent<Renderer>().material = PlaceGreen;
             // - same as in CollisionEnter do not 
             canPlace = true;
         }
     }
 }



screenshot-from-2015-04-09-094726.png (56.1 kB)
screenshot-from-2015-04-09-095047.png (56.0 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Move Object to location of Trigger? 1 Answer

How can I rotate my gameobject around z-axis correctly? 0 Answers

[Unity5 C#] OnTriggerEnter not working 1 Answer

Door Between Scenes 3 Answers

Tracking Player x Position for different things to happen not working 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