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 /
  • Help Room /
This question was closed Feb 15, 2018 at 01:14 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mintza · Jan 04, 2016 at 07:32 PM · gameobjectunity5getcomponentscript errorbool

How can i change bool of other gameobject?

Hi there, i have a first person character and when i press "E" key near a gameobject (boat) the character go in this gameobject. Now i want to move this boat and i have 3 bool created: CanControll, RotateLeft and RotateRight.

I want to change this bool from character controller script but i get error everytime.

Character controller script when i press E key:

 if (inputAccion) {
             //Debug.Log ("E Press");
             bool CloSeToBoat = CheckCloseToTag("Boat", 3);
             if(CloSeToBoat){
                 //Debug.Log("boat is near");
                 if(!OnBoat){
                     GameObject[] Boat;
                     Boat = GameObject.FindGameObjectsWithTag("Boat");
                     GameObject closest = null;
                     float distance = Mathf.Infinity;
                     Vector3 position = transform.position;
                     foreach (GameObject go in Boat) {
                         Vector3 diff = go.transform.position - position;
                         float curDistance = diff.sqrMagnitude;
                         if (curDistance < distance) {
                             closest = go;
                             distance = curDistance;
                         }
                     }
                     GameObject MyBoat = closest;
                     gameObject.transform.parent = MyBoat.transform;
                     gameObject.transform.localPosition = new Vector3(0, 15, -12f);
 
 
 
 
                     GameObject camera = GameObject.Find ("Main Camera");
                     camera.transform.localPosition = new Vector3(0, 15, -62f);
                     
                     gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
                     camera.transform.localRotation = Quaternion.Euler(0, 0, 0);
                     gameObject.GetComponent<MouseLook>().CanLook = false;
 
                     GameObject father =  gameObject.transform.parent.parent.gameObject;
                     father.GetComponent<BoatController>().CanControll = true;
                     
                     Debug.Log("Name: "+father);
 
                     OnBoat = true;
                     canControl = false;
                 }


I get error in this line:

father.GetComponent().CanControll = true;

Here the Boat Script:

 using UnityEngine;
 using System.Collections;
 
 public class BoatController : MonoBehaviour {
 
     public float BoatSpeed = 2;
     public float RotateSpeed = 15;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     public bool RotateLeft = false;
     public bool RotateRight = false;
     public bool CanControll = false;
     private Vector3 moveDirection = Vector3.zero;
 
     void Start(){
         CanControll = false;
     }
 
     void Update() {
         if (CanControll) {
             Debug.Log ("speed:"+ BoatSpeed);
             transform.Translate(Vector3.forward * (Time.deltaTime * BoatSpeed));
 
             if (RotateLeft) {
                 transform.Rotate(Vector3.up * (Time.deltaTime * RotateSpeed));
             }
             
             if (RotateRight) {
                 transform.Rotate(Vector3.down * (Time.deltaTime * RotateSpeed));
             }
         }
     }
 }


Please help me. Thak you

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 brunocoimbra · Jan 04, 2016 at 07:46 PM 0
Share

What error did you get?

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Denvery · Jan 04, 2016 at 07:50 PM

As I can see this expression father.GetComponent() returns null.

It means that the "father" object does not contain script "BoatController".

Are you sure that line

GameObject father = gameObject.transform.parent.parent.gameObject;

returns correct object?

You can check it in logs by writing line like this: Debug.Log("father name = " + father.name)

Comment
Add comment · Show 13 · 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 mintza · Jan 04, 2016 at 08:28 PM 0
Share

The log say me this: father name = BoatGal

BoatGal is the name of the gameobject that contains BoatController script.

avatar image Denvery mintza · Jan 04, 2016 at 08:33 PM 0
Share

Please write to log this line:

 Debug.Log("boat = " + father.GetComponent<BoatController>());

We need to check if it is == null

avatar image mintza Denvery · Jan 04, 2016 at 08:45 PM 0
Share

I cant execute it because i get this error:

error CS0246: The type or namespace name 'BoatController' could not be found. Are you missing a using directive or an assembly reference?

Show more comments
Show more comments
avatar image mintza · Jan 05, 2016 at 12:55 PM 0
Share

@Denvery Now im trying to get the script of the gameobject like this:

                 GameObject BoatTo$$anonymous$$ove = GameObject.Find("BOATGal1");
                 Debug.Log ("CanControll : "+BoatTo$$anonymous$$ove.GetComponent<BoatController>().CanControll);

Im geting the same error and i dont know why. The gameobject with name "BOATGal1" have a script into it names "BoatController":

alt text

I get this error in my console and i cant execute the game:

 Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.cs(438,78): error CS0246: The type or namespace name `BoatController' could not be found. Are you missing a using directive or an assembly reference?

Please help me. Thank you

script-into-gameobject.jpg (37.1 kB)
avatar image Denvery mintza · Jan 05, 2016 at 07:12 PM 0
Share

CharacterController can't see "BoatController". Try reimport BoatController script.

avatar image mintza Denvery · Jan 07, 2016 at 08:27 PM 0
Share

I solve it. I have not the "BoatController" script in the same directory of "Character$$anonymous$$otor" script. I move it and no it works.

But no i cant get the bool from BoatController script. Im using this script to get the bool:

Debug.Log ("Name : "+BoatTo$$anonymous$$ove.GetComponent().CanControll);

The console send me this error: NullReferenceException: Object reference not set to an instance of an object Character$$anonymous$$otor.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.cs:442)

Show more comments

Follow this Question

Answers Answers and Comments

45 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

Related Questions

Namespaces, referencing those namespaces, and everything in between. 0 Answers

Store script from GetComponent ? 1 Answer

Object reference; sometimes works but sometimes doesn't??? 2 Answers

GetComponent 1 Answer

Detecting for a variable from multiple potential scripts. 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