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 10:04 AM · c#unity 5variablebool

Variable Assigned Error along with "Name "" Not Assigned in Current Context

Hi, I'm using GetComponent to grab a bool from this script:

 using UnityEngine;
 using System.Collections;
 
 public class GameObjectDisable : MonoBehaviour {
     public Material PlaceRed;
     public Material PlaceGreen;
     public bool CanPlace;
 
     void Start (){
         GameObject GhostObject = GameObject.Find("planetstationtransparent(Clone)");
     }
 
     void OnCollisionEnter (Collision col) { //if in contact, change to red and make non placeable
         if(col.gameObject.name == "planetstationtransparent(Clone)")
         {
             GhostObject.GetComponent<Renderer>().material = PlaceRed;
             bool CanPlace = false;
         }
     }
     void OnCollisionExit(Collision col) { //if no longer in contact, change back to green and placeable
         if(col.gameObject.name == "planetstationtransparent(Clone)")
         {
             GhostObject.GetComponent<Renderer>().material = PlaceGreen;
             bool CanPlace = true;
         }
     }
 }

 And use it to see if it can pass the if statement in this script.
 
 
 using UnityEngine;
 using UnityEngine.Events;
 using System.Collections;
 
 public class Builder : MonoBehaviour {
      
      public GameObject BuildingFinal;
      public GameObject GhostObject;
      public Builder BuilderToggle;
      
 
 
     // Use this for initialization
     void Start () {
         GameObject Water = GameObject.FindWithTag("Water");
         //grabs itself so it can be disabled in the future
         BuilderToggle = GetComponent<Builder>();
         //makes translucent object with red and half opacity TODO: GET THIS TO WORK
         GhostObject = (GameObject)Instantiate(Resources.Load("planetstationtransparent"));
     }
     
     // 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;
              GhostObject.transform.position = Placement;
              GhostObject.transform.up = hit.normal; 
             //transform up is for rotation
             
             // previous code is for the raycasting of planet mesh
             if (Input.GetMouseButtonDown(0) && Water.GetComponent<GameObjectDisable>().CanPlace == true) {
                 BuildingFinal = (GameObject)Instantiate(Resources.Load("planetstation"));
                 BuildingFinal.transform.position = Placement;
                 BuilderToggle.enabled = false;
                 
          }
             if (Input.GetMouseButtonDown(1)){
                 GhostObject.SetActive(false);
                 BuilderToggle.enabled = false;
             }
         }
         
     }
 }

I posted the entire code because I'm not sure where the error lies. The first script goes on the Water GameObject, declaring that if it is in contact, make it not placeable, and Red

Comment
Add comment · Show 1
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 NoseKills · Apr 09, 2015 at 03:43 PM 0
Share

I don't know what you are asking but try not declaring (creating) a duplicate variable called CanPlace inside the OnCollisionEnter and Exit.

Change

 bool CanPlace = false;

to

 CanPlace = false;

With the bool word you are creating a new variable that only exists inside the function and disappears when you exit the function call.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GetComponent, What is it ? 1 Answer

Tracking Player x Position for different things to happen not working 0 Answers

Unexpected Symbol 'public' 1 Answer

Bool function not running when called from other script 2 Answers

How to make on/off-like gui-button? 3 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