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
1
Question by JesusChristChangedMe · Jun 09, 2011 at 11:33 PM · getcomponentaddcomponentignore

AddComponent not working

Hello, Ive had this problem before of Destroying a component then not being able to re-add it. I fixed it bygameObject.AddComponent(ScriptName); but this time its not working. Is there a way to fix this or ignore the script?

 function Update () 
 {
 if(GettingInVehicle.InVehicle==true)
 {
 Destroy (GetComponent (MouseLook));
 }
 if(Input.GetKey("o")&&GettingInVehicle.InVehicle)
 {
 gameObject.AddComponent(MouseLook);
 }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by JesusFreak96 · Jun 16, 2011 at 01:54 AM

you could just activate/deactivate it instead of destroying it. just make a static public bool (which is c# for boolean or true/false) in the mouselook script and make an if statement that says if you're not in the car, work. :

 function Update () 
 {
  if(GettingInVehicle.InVehicle)
  {
   MouseLook.inCar = true;
  }
  if(Input.GetKey("o")&&GettingInVehicle.InVehicle)
  {
   MouseLook.inCar = false;
  }
 }

and the MouseLook script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 /// MouseLook rotates the transform based on the mouse delta.
 /// Minimum and Maximum values can be used to constrain the possible rotation
 
 /// To make an FPS style character:
 /// - Create a capsule.
 /// - Add a rigid body to the capsule
 /// - Add the MouseLook script to the capsule.
 ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
 /// - Add FPSWalker script to the capsule
 
 /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
 /// - Add a MouseLook script to the camera.
 ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
 public class MouseLook : MonoBehaviour {
 
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 15F;
     public float sensitivityY = 15F;
 
     public float minimumX = -360F;
     public float maximumX = 360F;
 
     public float minimumY = -60F;
     public float maximumY = 60F;
     static public bool inCar = false;
     float rotationX = 0F;
     float rotationY = 0F;
     
     Quaternion originalRotation;
 
     void Update ()
     {
         if(!inCar)
         {
          if (axes == RotationAxes.MouseXAndY)
          {
              // Read the mouse input axis
              rotationX += Input.GetAxis("Mouse X") * sensitivityX;
              rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
 
              rotationX = ClampAngle (rotationX, minimumX, maximumX);
              rotationY = ClampAngle (rotationY, minimumY, maximumY);
             
              Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
              Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
             
              transform.localRotation = originalRotation * xQuaternion * yQuaternion;
          }
          else if (axes == RotationAxes.MouseX)
          {
              rotationX += Input.GetAxis("Mouse X") * sensitivityX;
              rotationX = ClampAngle (rotationX, minimumX, maximumX);
 
              Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
              transform.localRotation = originalRotation * xQuaternion;
          }
          else
          {
              rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
              rotationY = ClampAngle (rotationY, minimumY, maximumY);
 
              Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
              transform.localRotation = originalRotation * yQuaternion;
          }
         }
     }
     
     void Start ()
     {
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
         originalRotation = transform.localRotation;
     }
     
     public static float ClampAngle (float angle, float min, float max)
     {
         if (angle < -360F)
             angle += 360F;
         if (angle > 360F)
             angle -= 360F;
         return Mathf.Clamp (angle, min, max);
     }
 }
Comment
Add comment · Show 5 · 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 Peter G · Jun 16, 2011 at 02:53 AM 0
Share

I read your first sentence and I was like cool, but then I continued and I cringed. I know the Unity Docs make it sound this way, but static variables are not for passing information between scripts. You should almost always use GetComponent<$$anonymous$$yScript>().variableName. (the chevron brackets are taken out by QATO so just pretend that they're there after the GetComponent and before the () )

avatar image Jesus_Freak · Jun 16, 2011 at 03:26 AM 0
Share

well, it works. and the unityscript will talk to the c# that way. I can change a boolean from a javascript... then again, I was just messing around trying to fix a problem on my own, it actually worked for me.

avatar image Peter G · Jun 16, 2011 at 04:21 AM 0
Share

Right, it will work if you only have one $$anonymous$$ouseLook. But, static makes it a class variable which is not what you want. The question comes up all the time. Instance variables should not be marked static.

avatar image Jesus_Freak · Jun 18, 2011 at 07:01 AM 0
Share

well as a new c# programmer, I'd like to know the proper way to make c# talk to unityscript (and unityScript -> c#)

avatar image Jesus_Freak · Jun 18, 2011 at 07:01 AM 0
Share

... with static ... or instance variables.

avatar image
0

Answer by Nemox · Jun 09, 2011 at 11:37 PM

Try gameObject.AddComponent("Mouselook");

Comment
Add comment · Show 2 · 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 JesusChristChangedMe · Jun 09, 2011 at 11:40 PM 0
Share

sorry, didnt work

avatar image Bunny83 · Jun 16, 2011 at 02:11 AM 1
Share

That's the same just less effective...

avatar image
0

Answer by Anxo · Jun 16, 2011 at 01:58 AM

 private var myMouseLook;
 
 function Awake(){
 
  myMouseLook = GetComponent(MouseLook);
 
 }
 function DisableMouseLook(){
 myMouseLook.enabled = false;
 }
 function ActivatemouseLook(){
 
 myMouseLook.enabled = true;
 }
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Joints Configured from Script do not work properly? $20 reward 3 Answers

gameObject.GetComponent("Script").enabled = true not working 5 Answers

Get Component from Instantiated Prefab 1 Answer

AddComponent for RawImage not functioning as expected 0 Answers

script GetComponent, nullReference error 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